0

I'm trying to make a Wordpress plugin, therefore, I'm trying to manipulate my DOM (the post content). Currently I have this code:

<?php
function mb_find_my_image( $content ) {
    if( is_home() ) { /* if is home start */

      $dom = new domDocument;
      $dom->loadHTML($content);
      $dom->preserveWhiteSpace = false;


    } /* if is home end */
    return $content;
}
add_filter( 'the_content', 'mb_find_my_image' );
?>

But I always get the following error:

Empty string supplied as input

I have tried using any other custom string that has html elements and it works. Also I've tried using var_export($content, true ), and this somewhat fixes the problem, but I get errors such as:

Call to a member function getAttribute() on a non-object

What I'm I doing wrong? I'm running this script on my local computer using WAMP Server.

var_dump($content) gives me:

null
string post content (actual html that is the post) ... (length=805)

5
  • 1
    It seems like you are not passing the parameter correctly. Check where you are calling your function and see what's being passed in. Commented Apr 17, 2013 at 18:37
  • @KyleGibbens Could you please tell me how to do that? I'm still new to PHP and Wordpress. Commented Apr 17, 2013 at 18:39
  • Inside which file is your mb_find_my_image function located? Commented Apr 17, 2013 at 19:04
  • I'm not very familiar with Wordpress, but find where the function is being called from (CTRL+F 'mb_find_my_image' on your source code) and var_dump the value passed in. Again, I'm not very familiar with Wordpress. If Wordpress calls the function using that 'add_filter' function, then makes sure your content is valid HTML. Commented Apr 17, 2013 at 19:06
  • @dalbaeb this function is located in functions.php file Commented Apr 17, 2013 at 19:14

1 Answer 1

0

I have fixed the problem. The main problem was that I was calling the function (mb_find_my_image) from index.php, after I've removed the calling from index.php everything works as it should.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.