Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Pippins explains the_content filter and working with it pretty well in his blog post pippinsplugins.com/playing-nice-with-the-content-filter Hopefully it provides some insight. –  DesignerEthan Feb 18 at 18:07
add comment

1 Answer

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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