1

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:

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?

5
  • It would help if you described what else your plugin does. Eg. where else is it hooking in. Commented Oct 4, 2011 at 23:45
  • It has many different hooks as it does lots of different things. It has hooks that run at init, and others at wp, the_content, the_title, etc. Bottom line is I need to be able to bail out as early as possible (during wp I'm guessing?). And I can already do so, but like I said it just returns to the rest of the plugin and continues to execute rather than bailing out of my code.
    – JVC
    Commented Oct 4, 2011 at 23:49
  • 1
    It doesn't make any sense what you want. Remove the plugin hooks instead Commented Oct 5, 2011 at 0:53
  • Umm, how can you tell me that what I want doesn't make any sense? It makes perfect sense. Your failure to understand is no reflection on what I want to accomplish. If you don't understand what I'm asking, either request further clarification or move on to another question.
    – JVC
    Commented Oct 5, 2011 at 2:28
  • What are you doing on init ? The wp action(where you conditional logic is) occurs after init, so do you have anything happening before that action? You should be fine if you're adding actions/filters at that point as you can simply unhook/unregister them.. but you need to give more substance to your question. What you're asking is entirely possible, but without more details about what you're doing it's hard to provide a specific example..(a general one could be given but it may not address your use case).
    – t31os
    Commented Oct 6, 2011 at 13:26

1 Answer 1

0

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.

3
  • Yeah unfortunately I don't think that will work. If it's allowed to run all the way to the end then it has already done things to modify the page in various ways. It has to exit before it's done anything, if it's going to exit.
    – JVC
    Commented Oct 4, 2011 at 23:52
  • You're better off just checking the conditional inside each function you're hooking into. But be aware that conditional checks are not going to work early on (eg. init) because the query hasn't run. What's the end goal of this? Just to keep your code concise -- not write is_single inside every function? Commented Oct 4, 2011 at 23:56
  • Yeah I tried this approach after I wrote the question, and the problem is that some parts of my plugin are still executing but I can't figure out why. And yes the hope was to not make WP loop through every section of my plugin just to bail out at each one as it does the is_singular check. I'm OK with calling it in every function if I have to but right now even that's not behaving properly. Need to dig in and figure out why as it's quite strange. Appreciate the suggestion!
    – JVC
    Commented Oct 5, 2011 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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