0

I'm working on a custom theme that uses Buddypress and bbPress to add social network functionality. I've noticed that both plugins css and js (many files) are loaded on every page, even though none of the functionality is used there.

For example, in my homepage I only have a link to the forums, so I don't want all the scripts and styles included there.

I'm looking for something like

if (is_buddypress() || is_bbpress() ) {

    wp_enqueue_style(...);
    wp_enqueue_script(...);
}

Is there a way to tell these plugins to add their scripts and styles only when the conditional tags above are true?

Thanks in advance!

1

For this specific instance you could use is_front_page() and then dequeue them, which will remove them from the front page.

if(is_front_page()){
    wp_dequeue_script( ... );
    wp_dequeue_style( ... ); 
}

As for other pages, I would suggest having an array of pages to exclude the resources from and use is_page() to check if it should be excluded. Ideally this array would be set on an options page

$pages_to_exclude = array( 1,2,3... );
foreach($pages_to_exclude as $page){
    if(is_page($page)){
        wp_dequeue_script( ... );
        wp_dequeue_style( ... ); 
    }
}

Or the native PHP function in_array()

global $post;
$pages_to_exclude = array( 1,2,3... );
if(in_array($post->ID,$pages_to_exclude){
    wp_dequeue_script( ... );
    wp_dequeue_style( ... ); 
}

As for an inbuilt solution that comes with the plugins, I wouldn't know as I'm not familiar with them, so there may be a better solution out there. Not sure whether that would be considered within the scope of this site though.

  • This makes sense but I have no idea what the handles for all the styles and scripts are! – Alejandro Jul 3 '15 at 7:45

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