Why is my PHP variable created by my xpath query is throwing an “Object of class DOMAttr could not be converted to string” fatal error?

So I started a question to figure out why my PHP code that is meant to grab the only MP3 URL that exists within each of my Post contents on my WordPress installation for a custom use of the content here >> Why doesn’t my properly defined variable evaluate for length correctly (and subsequently work in the rest of my code)?

I have made several edits and updates and now need to re-formulate both the question and the context. User mrtsherman points out that based on the order I have written code, that I redefine $doc but don’t load that into $xpath, however when I try to adjust this my code throws the fatal error “Object of class DOMAttr could not be converted to string” on the line at the very end where I echo the variable $BEmp3s, the end result of all this. Does this mean it cannot convert the attributes to a string I think??

Read More

I know I am soo close to the solution here it’s nearly killing me but I also think I have been looking at this wayy too much and for too long. Any insight is golden at this point! Here is my code that branches correctly throughout now:

<?php
    // Start MP3 URL
    $doc   = new DOMDocument();
    $doc->strictErrorChecking = FALSE;

    $xpath = new DOMXpath($doc);
    // End MP3 URL

    $a = 1;
    if (have_posts()) :
        while ( have_posts() ) : the_post();
?>
<?php
$BEpost_content = get_the_content();
if (strlen($BEpost_content) > 0) {
    echo "<div id='debug_content'>get_the_content has something</div>";
} else {
    echo "<div id='debug_content'>BEpost_content is empty</div>" ;
};
$success = $doc->loadHTML($BEpost_content);
$xpath = new DOMXpath($doc);
if ($success === FALSE) {
    echo "<div id='debug_loadcontent'>loadHTML failed to load post content</div>";
} else {
    $hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href");
    if ($hrefs->length > 0) {
        echo "<div id='debug_xpath'>xpath found something</div>";
    } else {
        echo "<div id='debug_xpath'>xpath found nothing</div>";
    };
    $BEmp3s = $hrefs->item(0);
};
?>
<script type="text/javascript">
    var myCP<?php echo $a; ?> = new CirclePlayer("#jquery_jplayer_<?php echo $a; ?>",
    {
        mp3: "<?php echo $BEmp3s; ?>"
    }, {
        cssSelectorAncestor: "#cp_container_<?php echo $a; ?>",
        volume: 0.5
    });
</script>

Related posts

Leave a Reply

1 comment