How to stop your plugin from executing on certain pages?

Question

I’m building a plugin that extends WooCommerce and I want to prevent it from executing once it hits the template_redirect hook. My goal is to stop the plugin from loading all the assets on non-product pages. Any ideas on how to do this?

Code

add_action('template_redirect', array($this, 'stoplight'));

function stoplight() {
        $post_type = get_post_type();
        if ($post_type != 'product') {
            return;
            //I used wp_die() here as well, but it killed WordPress.
        }
    }

0
sackadelic 3 months 2022-05-08T09:06:42-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse