Support » How-To and Troubleshooting » [Resolved] Filters not returning modified content

[Resolved] Filters not returning modified content

  • Alex


    Howdy everyone, I’m running into a frustrating issue when adding a filter to the_content. While I know the code executes, the modified content doesn’t replace the original. I’ve hit a wall. I’m sure I’m missing something obvious, so I would appreciate any pointers.

    Here are the two filters from my child theme’s function.php, neither of which returns the new version of $content, though when I add an echo 'test'; anywhere within the functions, that is output, so I know these are being executed. If it matters, my child theme is based on the most recent version of the Genesis framework.

    /*! Link Twitter @usernames */
    function linktwitterhandles($content) {
    	$content = preg_replace('/([\.|\,|\:|\Á|\À|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/$2\" class=\"tweet-username\">@$2</a>$3 ", $content);
    	return $content;
    add_filter('the_content', 'linktwitterhandles', 19);
    add_filter('the_excerpt', 'linktwitterhandles', 19);
    add_filter('get_comment_text', 'linktwitterhandles', 19);
    /*! Post Tweaks for Pinterest */
    function custom_pinterest_content($content) {
    	if ( is_syndicated() && has_tag('pinterest') ) {
    		$content = str_replace('<img', '<a href="'. get_syndication_permalink() . '"><img', $content);
    		$content = str_replace('/></p>', '/></a></p>',$content);
    	return $content;

    In both samples, when I echo $content right before I return it, I see double results, the first of which is the modified version, the second is the original. I’ve changed the execution order, using values as low as 1 and as high as 2000 for both add_filter lines.

    Any thoughts?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi, maybe you conditions are not true i.e.

    $content = preg_replace('/([\.|\,|\:|\Á|\À|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/$2\" class=\"tweet-username\">@$2</a>$3 ", $content);


    if ( is_syndicated() && has_tag('pinterest') ) {

    Try to remove these conditions to test.

    or did you get echo "hello"; from there too?

    I guess I would try to start with something like:

    function test_filter1($content) {
            // die("Debug: filter nr 1");
    	return $content." <hr><pre>(Hello I'm filter nr 1)</pre> <hr>";
    function test_filter2($content) {
            // die("Debug: filter nr 2");
    	return $content." <hr><pre>(Hello I'm filter nr 2)</pre><hr>";
    add_filter('the_content', 'test_filter1', 19);
    add_filter('the_content', 'test_filter2', 20);

    you can check out all the filters hooked into “the_content” with this:

    function show_all_filters() {
        global $wp_filter;
        echo '<pre>';
        echo '</pre>';


    Hi birgire, thanks for the fast reply and helpful code snippets. Both filters are executing properly and the function logic is correct. When I echo $content within the function, I see the post content twice – the first being the modified content, the other being the original, non-modified content. I’ve actually echoed it out prior to the logic, within the logic and right after the logic, before the return, and the rendered page shows original content, modified content, original content.

    Your show_all_filters snippet is very useful, thanks! It shows that custom_pinterest_content is executed on a single post as expected. The problem appears to only be that return $content isn’t overwriting the actual post content when output.

    and are you using the the_content() template tag in your theme?

    because sometimes I have seen <?php echo $post->post_content;?> used instead 😉

    or mabye you have some other the_content() filters that are using echo instead of return ?



    I eventually tracked it down to how FeedWordPress implements it’s filters. Removing them did the trick:

    /*! Disable FeedWordPress Filters */
    function clean_up_feedwordpress() {
    		remove_filter('the_content', 'feedwordpress_preserve_syndicated_content', -10000);
    		remove_filter('the_content', 'feedwordpress_restore_syndicated_content', 10000);

    This could cause issues for others, depending on the source of the feed, but I can control what comes through my feeds, so no major issues,

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Filters not returning modified content’ is closed to new replies.