How to exit a plugin’s execution mid-stream?

I have a plugin that I want to ensure runs ONLY on singular posts, pages, etc. I’m using the is_singular() function and it works great – except that all it does is return execution to the rest of the plugin if I do this:

function singular_check(){
    if(is_singular())
        return;
}

I’m executing it in a hook like so:

Read More
add_action('wp', 'singular_check');

I need to actually make my entire plugin bail out at that point, and return to executing whatever else was downstream of my plugin. Anyone know how to do so?

Related posts

Leave a Reply

1 comment

  1. A possible solution would be to hook in way late (template redirect, for example), do your conditional check. If it passes, include (or require) a file that contains the stuff needed.

    <?php
    add_action( 'template_redirect', 'wpse30291_template_redirect' );
    function wpse30291_template_redirect()
    {
        if( ! is_singular() ) return;
        require_once( 'your-file.php' );
    }
    

    This is not going to work if your-file.php contains functions hooked into things that happened earlier (eg. init or wp). But if your-file.php contains things that, say, modified content (hooking in the_title or the_content, for instance) then it would be fine.