There are two parts to this:
1. When to disable Toolset assets
2. Which assets to disable
We could take a whitelist approach, i.e. dequeue scripts on all pages except specified pages, or a blacklist approach, i.e. only dequeue scripts on specific pages.
The whitelist approach makes sense where Toolset is used in only a few parts of a site, while the blacklist approach makes sense where Toolset is used extensively but where pages such as landing pages need to be optimised for SEO.
1. When to disable Toolset assets
When visiting the front end of a site we are either viewing pages, posts (of some type), or archives of posts.
We can use conditional tags to test which: https://developer.wordpress.org/themes/references/list-of-conditional-tags/
The key tags are
- is_page() : test if a static page generally, a specific page, or one of an array of pages
- is_single() : test if a single post (of whatever type) generally, a specific post, or one of an array of posts
- is_singular() : test if a single post of any type, or of one of the specified types
- is_archive() : tests if any kind of archive being displayed except blog post archive
- is_home() : tests for blog post archive
- is_post_type_archive() : tests if an archive of some CPT
- is_tax(), is_category(), is_tag() : tests for if on a taxonomy archive generally, or with specified term(s)
2. Which assets to dequeue
I've taken a fairly simply approach here, looking at what assets are enqueued on pages where there is no Toolset content and so which are unnecessary.
There is not much of an alternative to examining the registered scripts and checking their dependencies to work out which scripts to dequeue. The reason is that some core scripts which are loaded as dependencies of Toolset, e.g. underscore.min.js, may be needed by scripts loaded by other plugins. Removing the Toolset scripts that require them will remove such scripts—unless something else is calling them.
With Types, Views, Access, Forms, Maps and Layouts active all of the unnecessary scripts on pages with no Toolset content can be removed by dequeueing the following:
wpv-pagination-embedded.js
main.js
ddl-tabs-cell-frontend.js
date.js
bootstrap.min.js
(Note there is an active issue in WP core about the media element scripts being added to the head instead of the footer: https://core.trac.wordpress.org/ticket/44484.)
So a whitelisting example might look like this:
function whitelist_toolset_scripts( ){
// Remove Toolset scripts everywhere except specified pages and custom archive for listing post type
if ( !is_page( array('page-with-a-view', 'another-view-page', 'custom-login-page', 'submit-posts-form' ) ) && !is_post_type_archive( 'listing' ) ) {
wp_dequeue_script( 'views-pagination-script' );
wp_dequeue_script( 'wptoolset-field-date' );
wp_dequeue_script( 'toolset_bootstrap' );
wp_dequeue_script( 'ddl-tabs-scripts' );
}
}
add_action( 'wp_print_scripts', 'whitelist_toolset_scripts', 9999 );
In this example we want to get rid of Toolset scripts everywhere, except for certain pages where we know Toolset is used, including a custom archive we made for the listing post type.
Similarly, here's what a blacklist example might look like:
function blacklist_toolset_scripts( ){
// Only remove Toolset scripts on specific pages and all archives including blog archive
if ( is_page( array('sample-page', 'landing-page-1', 'landing-page-2', 'contact-us', 'home' ) ) || is_archive() || is_home() ) {
wp_dequeue_script( 'views-pagination-script' );
wp_dequeue_script( 'wptoolset-field-date' );
wp_dequeue_script( 'toolset_bootstrap' );
wp_dequeue_script( 'ddl-tabs-scripts' );
}
}
add_action( 'wp_print_scripts', 'blacklist_toolset_scripts', 9999 );
Here the scripts are only dequeued on particular targeted pages and, in this example, all archives.
Every site is different and so there is no one-size-fits-all solution, you'll need to think about which approach makes most sense for your site, and be sure to test so that if you are removing Toolset assets you don't accidentally break the functionality of some page where you have mistakenly disabled the Toolset scripts.
You could try to make the process of identifying where to exclude scripts simpler by, for example, adding a checkbox custom field to all pages and post types for whether to exclude assets for the page/post...