4

I am creating a WordPress Plugin for WordPress directory.

How can I get the_content() after applying all the shortcodes that are presents in the_content?

Let me explain:

My plugin will be used in multiple themes and websites; and users will add some shortcodes in their posts or pages. I want my plugin to work after these shortcodes are parsed, and then use the content for my plugin as input.

3

Are you looking for a filter perhaps?

add_filter( 'the_content', 'wpse_the_content_filter', 20 );

function wpse_the_content_filter( $content ) {
   // Do whatever you want with the $content
   return $content;
}

Filtering 'the_content' will pass the post or page content through a function of your choice.

Just make sure to run it late (e.g. 20) and return the contents so they can be displayed.

More information here:

https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

2

First rule:

When used within theme's template files (i.e. WordPress loop), the_content() will by default return the content after applying all the shortcodes.

Second rule:

If you have any custom shortcode, you must have shortcode handler function applied with add_shortcode (obviously). Otherwise your own custom shortcodes will not work.

Third rule:

If your plugin's content related function is running independently from the loop and you want to retrieve the content using custom query, then to apply the shortcodes in the content, you may use do_shortcode function. Like:

$content = do_shortcode( $content );

or, you may use the_content filter for the same purpose:

$content = apply_filters( 'the_content', $content );

Here you'll find a similar discussion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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