Skip Navigation

[Resolved] Unnecessary load of CodeMirror/Select2 in frontend, when using Forms

This support ticket is created 4 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 0.12 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by adrianM-6 4 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#1665371

Hello,

We are using Forms to display some basic contact forms, only basic input and textarea.

Unfortunately this also loads some Javascript, i.e. CodeMirror and Select2 into the frontend.

CodeMirror more than all of our other scripts together and bloats our loading times (even with caching and js optimization).

Select2 is quite a large lib, we don't use too.

Together the seem to be about 2/3 of all of our JavaScripts.

So my suggestions:

Only load these scripts needs if needed or allow them to be disabled.

Thanks,

Adrian

#1666185

Hi Adrian,

Thank you for contacting us and I'd be happy to assist.

We have an internal ticket to introduce more efficient loading of scripts and styles in future releases, but I'm afraid, I don't have a time estimate to share the moment.

For now, a workaround can be to use some custom code to dequeue and deregister the unnecessary scripts.

Example:


add_action( 'wp_enqueue_scripts', 'remove_default_scripts', 20 );

function remove_default_scripts() {   
	wp_dequeue_script( 'toolset_select2' );
	wp_deregister_script( 'toolset_select2' );
	wp_dequeue_script( 'toolset-codemirror-script' );
	wp_deregister_script( 'toolset-codemirror-script' );
}

The above function will remove the "select2" and "codemirror" scripts from all front-end pages. To make this code remove these scripts only from the specific page(s), you can introduce conditions through WordPress conditional tags:
https://codex.wordpress.org/Conditional_Tags

Tip: You can use the "toolset_add_registered_script" filter in a temporary function to view the handles of all the scripts that are loading:


add_filter( 'toolset_add_registered_script', 'custom_filter_to_view_scripts' );
function custom_filter_to_view_scripts( $scripts ) {
	
	print_r($scripts);

	return $scripts;
}

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1666361

Hello Waqar,

Thank you, this is okay for me now.

Looking forward to an integrated solution in a future release.

Thanks,

Adrian