Skip Navigation

[Closed] Load Bootstrap only on necessary pages

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by Christopher Amirian 1 year, 2 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2640133

I've adjusted the Settings to load bootstrap but I only need the multiple bootstrap files that load on 2 pages of my site, so I'd like to only load it on those pages. How can I do this?

Thanks,

Tim

#2640479

Christopher Amirian
Supporter

Languages: English (English )

Hi Tim,

You can use this method to load the assets selectively:

https://toolset.com/forums/topic/expand-function-to-include-bootstrap-css-on-specific-page/

Thanks.

#2640629

Thanks, I followed that post and adjusted the code slightly but it doesn't seem to be working. Bootstrap is still loading sitewide.

Here's the code I am using:

function dequeue_bootstrap_except_necessary(){
  // a comma-separated list of post type slugs where you want to include Bootstrap
$cpts = array( 'realtor', 'realtors', 'testing' );  if ( !in_array( get_post_type() , $cpts) && !is_admin()  ) {
  
    // dequeue Bootstrap CSS
    function dequeue_bootstrap_css(){
      wp_dequeue_style( 'toolset_bootstrap_4' );
      wp_dequeue_style( 'toolset_bootstrap_styles' );
    }
    add_action( 'wp_print_styles', 'dequeue_bootstrap_css' );
  
    // dequeue Bootstrap JS
    function dequeue_bootstrap_js(){
      wp_dequeue_script( 'toolset_bootstrap_4' );
      wp_dequeue_script( 'toolset_bootstrap' );
    }
    add_action( 'wp_print_scripts', 'dequeue_bootstrap_js');
  
  }
  
}
add_action( 'wp_enqueue_scripts', 'dequeue_bootstrap_except_necessary', 100 );
#2640983

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

To debug the code I would first check if it is possible to dequeue the CSS and JS files site wide without the if clause.

If yes, then I would have if clause with && operators instead of having the post type slugs added as an array.

If the issue persists, I'd appreciate it if you could give me the URL/User/Pass of your WordPress dashboard after you make sure that you have a backup of your website.
It is absolutely important that you give us a guarantee that you have a backup so if something happens you will have a point of restore.

Make sure you set the next reply as private.

I can take a look into that, please tell me where you added the custom code?

The topic ‘[Closed] Load Bootstrap only on necessary pages’ is closed to new replies.