Ensuring a plugin is loaded/run last?

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.

2 Answers
2

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.

Leave a Comment