3

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.

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.

3
  • As Kero says below you could also hook into shutdown but it really depends on what you are trying to accomplish with your plugin, if you can provide details about what the plugin does that would be very helpful.
    – WPExplorer
    Jul 24 '17 at 19:54
  • Added more context
    – warren
    Jul 24 '17 at 20:19
  • I guess if they're ocming in via javascript, there's not much I can do ... but using the PHP_INT_MAX option looks like it would work well. I'll give that a try. Thanks
    – warren
    Jul 25 '17 at 0:16
0

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.

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.