the content - Proper use of the_content filtering

admin2025-01-08  3

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.

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 edited Apr 2, 2014 at 14:00 stealthyninja 1,1301 gold badge15 silver badges21 bronze badges asked Jul 30, 2012 at 14:39 orionrushorionrush 9202 gold badges13 silver badges25 bronze badges 1
  • 1 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 Commented Feb 18, 2014 at 18:07
Add a comment  | 

1 Answer 1

Reset to default 0

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;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265714a1061.html

最新回复(0)