Proper use of the_content filtering

This is a bit difficult without posting scads of code, but my question is: What are the benefits of filtering through apply_filters('the_content', 'anything');, and given my example loop, am I filtering the_content excessively or inefficiently, or is there a better way?

I ask because I just tried the Shareaholic plugin, which is supposed to detect the end of the post and insert social links. In my loop however, I find that it injects itself each time apply_filters('the_content', 'anything'); is called, which in my case is quite a lot.

Because I’m new to WP and PHP, and Shareaholic is an established service, I just assume these problems are due to ignorance on my part. Shareaholic has a shortcode that I can implement directly into the template, but I just want some general feedback / validation / criticism on my approach, and any ideas how to clean up or what may be causing the issue.

My loop looks something like this:

    <?php /* Start loop */ ?>
    <?php while (have_posts()) : the_post(); ?>

    <?php function_for_post_banner() /* filters 'the_content' to add a post banner */ ?>

    <?php get_template_part('templates/loop', 'switch');
    /* Switch tests for post type and applies conditional markup, handels post type media and calls 'the_content' specific to each case. */ ?>

    <? fuction_attachements();
    /* do conditional formatting with post attachments ie images, media & docs all filtered through the_content  */ ?>
    <?php /* End Loop */ ?>

Any advice on this is much appreciated — thanks.

1 Answer
1

You want to add a filter to the filter queue on the_content:

add_filter('the_content', 'anything');

WordPress core then calls apply_filters within the_content function to run all filters in the queue:

function the_content( $more_link_text = null, $strip_teaser = false) {
    $content = get_the_content( $more_link_text, $strip_teaser );
    $content = apply_filters( 'the_content', $content );
    $content = str_replace( ']]>', ']]&gt;', $content );
    echo $content;
}

Leave a Comment