2

I'm writing a plugin that requires adding to the content; adding a filter to the_content() is straightforward enough, but the theme I'm testing in uses get_the_content() to build the page. There's an indication that it should be possible in the Wordpress codex but I don't understand the example:

If you use plugins that filter content (add_filter('the_content')), then this will not apply the filters, unless you call it this way (using apply_filters):

apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))

Can anyone help / explain?

Thanks,

David.

DavidRobins
  • 31
  • 1
  • 4

2 Answers2

1

What they're saying is you'd have to add code to the place that get_the_content() is called. From your description, that's in the theme - you'd have to change the theme's get_the_content() call as described.

The reason for that is that there are no filters in the get_the_content() function that you can hook into with your plugin (have a look at the source - there are no calls to the apply_filters() function in get_the_content(), except one for the "more" link text).

Hobo
  • 7,536
  • 5
  • 40
  • 50
0

Late to the party, but here it goes:

In your functions.php you can add something like this:

function do_filter_stuff($content) {
    //Whatever you want to do with your content
}
add_filter( 'the_content', 'do_filter_stuff' );

Next, in your page.php (or whichever template you want to use it in) you can call the raw content with the same filter as following:

<?php echo do_filter_stuff(get_the_content()); ?>
Nick van H.
  • 358
  • 1
  • 11
  • Why do you add a filter in the first place, when you're calling the filter function directly? And if you're able to change the template, you don' need a filter at all. – z80crew Jul 17 '20 at 08:21
  • 1
    Because according to the codex: "An important difference from the_content() is that get_the_content() does not pass the content through the the_content filter. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things." https://developer.wordpress.org/reference/functions/get_the_content/ – Nick van H. Jul 20 '20 at 12:07
  • Ah, I get the idea now: When `the_content()` is used in the theme, the filter is used. For all occurrences of `get_the_content()` you're calling the filter function by hand. (Of course you still have to use a child theme and could replace all calls of `the_content()`, too. Which would IMHO be more consistent.) – z80crew Jul 21 '20 at 09:42