2

I'm writing a plugin that adds some custom content near the end of a post (or page). So I've added a filter for 'the_content', and the plugin adds its content there. Mostly, it works.

However, some sites show the entire content of several posts on the homepage (rather than just excerpts). On those sites, my filter is being called once for every post that is displayed. So the custom content shows up more than once.

Is there some way for me to detect when my filter is a nested call? I.e. that it is being called for the content of a post which is being displayed on the homepage. I could then add my custom content only when it is a "top level" call.

I also considered attaching my filter to the footer instead of the content, but I've run across themes that don't have a footer.

Thanks! Ken

1 Answer 1

2

Single filter call

Just give this plugin a try. It illustrates the whole technique.

<?php
/** Plugin Name: (#69351) Example single filter call on <code>'the_content'</code> */
function wpse69351_single_call( $content )
{
    // Only for single view request (post, page, etc.)
    if ( ! is_singular() )
        return $content;

    // This removes the filter during its first call
    remove_filter( current_filter(), __FUNCTION__ );

    $custom = '<h1>I am only here once!</h1>';

    return $content.$custom;
}
add_filter( 'the_content', 'wpse69351_single_call' );

If you're not targeting the content itself, but more the LOOP, then try this example plugin.

<?php
/** Plugin Name: (#69351) Example single filter call on <code>'loop_start'</code> &amp; <code>'loop_end'</code> */
function wpse69351_single_call( $wp_query )
{
    // Abort in some case
    if ( 'post' !== get_query_var( 'post_type' ) )
        return;

    // This removes the filter during its first call
    remove_filter( current_filter(), __FUNCTION__ );

    return print '<h1>I am only here once!</h1>';
}
// Attach something to the START of the loop
add_action( 'loop_end', 'wpse69351_single_call' );
// Attach something to the END of the loop
add_action( 'loop_start', 'wpse69351_single_call' );

Incomplete Themes

I also considered attaching my filter to the footer instead of the content, but I've run across themes that don't have a footer.

Never ever even dare to think about caring for incomplete themes. We got Theme development guidelines for a reason and a call to wp_footer(); is a must have for every theme. Else, just think "it's crap!" by yourself and move on.

4
  • Thanks, that's some progress. It only runs once now. Problem is, it runs on the first post that is shown on the homepage, instead of on the homepage itself. In other words, I get this result on the homepage... post 1 content, custom content, post 2 content, post 3 content, <-- custom content should go here, at the end of the homepage
    – Ken
    Commented Oct 16, 2012 at 2:21
  • @Ken homepage (or anything else archive-like) is collection of posts, not a post by itself. There are very few hooks that are common to such elements, so you are out of generic options beyond footer... Alternately you can add your content to last post but that might not be very generic looking either, depending on theme.
    – Rarst
    Commented Oct 16, 2012 at 18:43
  • @Ken See edit + new example plugin.
    – kaiser
    Commented Oct 16, 2012 at 21:25
  • @Rarst Thanks for the help and commenting. Anyway, needed to do something with loop_start/*_end as I discovered them today (again) ;)
    – kaiser
    Commented Oct 16, 2012 at 21:26

Your Answer

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

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