Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there any way I can use is_single() inside of my plugin's functions.php file? Currently, this is what my code looks like:

if(is_single()) :
    function my_template() {
        include(PLUGINDIR . '/supersqueeze/all.php');
        exit;
    }
    add_action('template_redirect', 'my_template');
endif;

But for some reason, it is not working at all. If I remove if(is_single()), it works but is for all pages.

And then once I get that working, I'll need to filter it once more to see if the post has a certain custom field value, lets say the name will be Design and the value will be Custom.

Thanks in advance for anyone who can help me out.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The problem with your code is that you're checking is_single() when your plugin is first loaded, before the global query has run, so is_single() still returns false. You should move the is_single() check within your my_template function:

function my_template() {
    if(is_single() && have_posts() && 'Custom' == get_post_meta(get_the_ID(), 'Design')) {
    include(PLUGINDIR . '/supersqueeze/all.php');
    exit;
    }
}
add_action('template_redirect', 'my_template');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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