admin管理员组

文章数量:1122832

How can you ensure a given plugin is run last before the page finishes rendering?

Can it be ensured?

Specifically, I am writing a plugin that I want to post-process all content of a given post/page (after any formatting, extra links, ad injections, etc). The rest of the blog doesn't need to be processed - just posts & pages.

How can you ensure a given plugin is run last before the page finishes rendering?

Can it be ensured?

Specifically, I am writing a plugin that I want to post-process all content of a given post/page (after any formatting, extra links, ad injections, etc). The rest of the blog doesn't need to be processed - just posts & pages.

Share Improve this question edited Jul 24, 2017 at 20:19 warren asked Jul 24, 2017 at 19:20 warrenwarren 2812 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

You'll want to hook into "the_content" filter at a very high priority. Example:

function my_alter_the_content( $content ) {
    if ( in_array( get_post_type(), array( 'post', 'page' ) ) ) {
        // Do stuff here for posts and pages
    }
    return $content;
}
add_action( 'the_content', 'my_alter_the_content', PHP_INT_MAX );

Using the PHP_INT_MAX constant for the hook priority you can ensure it runs as last as possible (most plugins/themes will just use the default priority of 10).

The issue is when you say "after all ad injections...etc" that really depends how things are being added. Because if there are ads being added via javascript then of course the only way to override it would be via javascript.

Depending on what exactly you are trying to achieve, the shutdown action might be the place to hook into as it

Fires just before PHP shuts down execution.

本文标签: orderEnsuring a plugin is loadedrun last