WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Filters not returning modified content (7 posts)

  1. Alex
    Member
    Posted 1 year ago #

    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;
    }
    add_filter('the_content','custom_pinterest_content',11);

    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?

  2. birgire
    Member
    Posted 1 year ago #

    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);

    and

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

    Try to remove these conditions to test.

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

  3. birgire
    Member
    Posted 1 year ago #

    I guess I would try to start with something like:

    function test_filter1($content) {
            //debug
            // die("Debug: filter nr 1");
    	return $content." <hr><pre>(Hello I'm filter nr 1)</pre> <hr>";
    }
    function test_filter2($content) {
            //debug
            // 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);
  4. birgire
    Member
    Posted 1 year ago #

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

    function show_all_filters() {
        global $wp_filter;
        echo '<pre>';
        print_r($wp_filter['the_content']);
        echo '</pre>';
        exit();
    }
    
    add_action('wp','show_all_filters');
  5. Alex
    Member
    Posted 1 year ago #

    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.

  6. birgire
    Member
    Posted 1 year ago #

    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 ?

  7. Alex
    Member
    Posted 1 year ago #

    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);
    }
    add_action('wp','clean_up_feedwordpress');

    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,

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags