Improve pagespeed by removing javascript and css on non-Paytium pages
-
Hi David,
I noticed that quite a lot of css an javascript for Paytium was loaded on ALL my pages.
Inclass-paytium.php
there is a provision trying to prevent this from happening.
However the feature switchpaytium_always_enqueue
is turned on by default.
The corresponding description says‘Useful if using shortcodes in widgets or other non-standard locations.’
.
After switching
paytium_always_enqueue
off no css or javascript was loaded at all.
I use paytium shortcode in a html-markup block provided by my theme, which is non-standard, so finding the shortcode using strpos( $post->post_content, ‘[paytium’ ) !== false does not work, and all my Paytium-forms failed to load correctly.Then I found this posting https://wordpress.stackexchange.com/a/254591
It suggests a new and better way to enqueue styles and scripts.So I altered
add_filter( 'the_posts', array ( $this, 'load_scripts' ) );
to
add_filter( 'do_shortcode_tag', 'load_scripts', 10, 3);
and changed load_scripts to:
function load_scripts($output, $tag, $attr) { if('paytium' == $tag) { //make sure it is the right shortcode // Load CSS wp_enqueue_style( 'paytium-public' ); wp_enqueue_style( 'paytium-jquery-ui' ); // Load JS wp_enqueue_script( 'paytium-public' ); wp_enqueue_script( 'paytium-parsley' ); wp_enqueue_script( 'paytium-parsley-nl' ); // Localize the site script with new language strings wp_localize_script( 'paytium-public', 'paytium_localize_script_vars', array ( 'admin_ajax_url' => admin_url( 'admin-ajax.php' ), 'amount_too_low' => __( 'No (valid) amount entered or amount is too low!', 'paytium' ), 'subscription_first_payment' => __( 'First payment:', 'paytium' ), 'field_is_required' => __( 'Field \'%s\' is required!', 'paytium' ), 'processing_please_wait' => __( 'Processing, please wait...', 'paytium' ), ) ); } return $output; }
Also I’ve moved this function to just above
add_filter( 'do_shortcode_tag', 'load_scripts', 10, 3);
, otherwise the callback isn’t defined before it is called.Now all css and javascript are loaded only on pages with Paytium forms, improving my pagespeed on all other pages!
Would you please consider implementing this in a future version?
Thanks,
Rob
- The topic ‘Improve pagespeed by removing javascript and css on non-Paytium pages’ is closed to new replies.