0

To increase the loading speed, I would like to only enable Event Manager plugin on the pages where it's actually used. For that, I wrote this piece of code:

// deregister unnecessary plugin scripts:
function my_deregister() {
    if ( !is_page('events') ) { 
        wp_deregister_script( 'events-manager' ); 
    }
}

add_action( 'wp_print_scripts', 'my_deregister', 100 );

However, the problem is that this event plugin creates a single page for each event and it needs to be enabled on those sub-pages, too. Apparently, each generated page is named differently. For instance, www.mysite.com/events/EventNameEventDate.

So I would like to have the plugin disabled on all pages except for 'events' and its sub-pages (of which I will obviously not always know the count and names). The is_page() works fine for the Events page but how do I modify it so that it recognises its sub-pages and enables the plugin scripts on them, too?

This was just a plugin specific example for easier understanding of the problem, but the question is generic and plugin independent.

  • 1
    Try like is_singular( 'events' ) for single pages – Pratik Patel Sep 28 '18 at 9:08
  • Thanks, but that didn't work @PratikPatel – puk789 Sep 28 '18 at 9:11
  • events is a custom post type right ? – Pratik Patel Sep 28 '18 at 9:15
  • It might be smarter to turn it around, so so it's easier to maintain. You can hook into enqueue scripts, deregister the s cript be default and only enqueue it where it's needed. – Jeffrey von Grumbkow Sep 28 '18 at 9:17
  • @PratikPatel Events is a regular page and its subpages are generated by the plugin so not sure what it is to be honest. – puk789 Sep 28 '18 at 9:19
0

You can get parent page slug and check like

global $post;
$post_data = get_post($post->post_parent);
$parent_slug = $post_data->post_name;
//echo $parent_slug;
if ( !is_page('events') || $parent_slug != 'events') {

Please try and let me know if any query

0

As Pratik Patel said in one if his comments. You can use is_singular( 'custom_post_type_name' ) to filter on CPT pages.

Since you are using Events Manager see this file from their plugin:
https://plugins.trac.wordpress.org/browser/events-manager/trunk/em-posts.php
On line 3 you see they define their CPT name as event.
So in your case you can use is_singular('event')

// deregister unnecessary plugin scripts:
function my_deregister() {
    if ( ! is_page('events') && ! is_singular('event') ) { 
        wp_deregister_script( 'events-manager' ); 
    }
}

add_action( 'wp_print_scripts', 'my_deregister', 100 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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