0

When I use add_action( 'the_content'...

The content of the page just won't load. Also the content I am trying appears in all kinds of places in the page. Why?

Is there a way to force it to only appear under my post?

function __construct()
{
    add_action( 'wp_footer', array(&$this, 'loadRatingsJs') );
    add_action( 'wp_ajax_valnetincGetRatings', array(&$this, 'getRatings' ) );
    add_action( 'wp_ajax_nopriv_valnetincGetRatings', array(&$this, 'getRatings' ) );
    //add_action( 'the_excerpt', array(&$this, 'getRatings' ) );
    add_filter( 'the_content', array(&$this, 'getRatings' ) );
}

public function getRatings($content)
{
    //echo $postID;
    if(function_exists('the_ratings')) {
        /*if(is_null($postID))
        {
            the_ratings();
        } else {
            the_ratings('div', $postID);
        }*/
        $rating = the_ratings();
        return $content.$rating;
    }
}
  • 2
    your question is a bit confusing, it would help to see the actual relevant code. what does the function you hook to the_content action do? do you call the_content function in your loop? – Milo Jan 13 '14 at 15:47
2

the_content is not an action, it is a filter. You should be concatenating a string and returning it. It sounds like you are echoing content directly.

That is:

// wrong
function test_content($content) {
  echo 'this is wrong';
}
add_filter('the_content','test_content');

// right
function test_content($content) {
  return 'this is right '.$content;
}
add_filter('the_content','test_content');
  • the fact that add_action and add_filter are interchangeable adds a nice layer of confusion. – Milo Jan 13 '14 at 16:29
  • Using filter works. But again the new content appears in many places in the page. I just want it to appear for the post content. Also it now only shows on top of the post and not at the bottom like I would like. I tried to interchange the var but nothing works. – jnbdz Jan 13 '14 at 16:54
  • @Milo : I completely agree. add_action and add_filter should be distinct. – s_ha_dum Jan 13 '14 at 17:17
  • @Jean-NicolasBoulayDesjardins : you are going to have to post your code if you want a more specific response to your question. – s_ha_dum Jan 13 '14 at 17:17
1

One potential problem is that you don't return anything if your conditional fails:

public function getRatings($content)
{
    //echo $postID;
    if(function_exists('the_ratings')) {
        /*if(is_null($postID))
        {
            the_ratings();
        } else {
            the_ratings('div', $postID);
        }*/
        $rating = the_ratings();
        return $content.$rating;
    }
}

To simplify:

public function getRatings($content)
{
    // Conditional
    if(function_exists('the_ratings')) {
        // Do stuff
        // Return modified content
        return $content.$rating;
    }
}

The problem is: you don't return anything if the_ratings() function doesn't exist. You need to account for that condition:

public function getRatings($content)
{
    // Conditional
    if(function_exists('the_ratings')) {
        // Do stuff
        // Return modified content
        return $content.$rating;
    }
    // Condition is false;
    // Just return the original content
    return $content;
}

An even simpler way (perhaps) is to return outside the conditional, and only modify inside the conditional:

public function getRatings($content)
{
    // Conditional
    if(function_exists('the_ratings')) {
        // Do stuff
        // Modify content
        $content .= $rating;
    }
    // Return content, modified or unmodified
    return $content;
}
  • Actually the the_rating does output something. What used to not output anything was the post... But now it works. the only problem I still have is that the_ratings() content does not output at the bottom of the post but at his top. It also appears at other places in the page. – jnbdz Jan 13 '14 at 19:01
  • Please edit your question and add those clarifications - especially as they relate to the up-voted comment. – Chip Bennett Jan 13 '14 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.