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.

1 Answer
1

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');

Leave a Reply

Your email address will not be published. Required fields are marked *