Open
Description
Details
Google Page Speed really doesn't like the 250 kB or so of JS, CSS, Polyfill.js etc. (not counting addons) that load on every page of the website as soon as you enable Give, even on pages where no forms are present. Right now, scripts are loaded in a flat way through Give_Scripts::init()
.
Additional Context
https://developers.google.com/web/updates/2018/07/search-ads-speed
Acceptance Criteria
- The statics should load only where relevant.
Hints
There are 2 ways it can be done :
- calling
Give_Scripts::public_enqueue_styles()
andGive_Scripts::public_enqueue_scripts()
inside the shortcodes and widget functions, rather than inGive_Script::init()
, - filter the page content after shortcodes have rendered, to search for Give objects, like :
add_filter('the_content', 'enqueue_give_statics', 20); // shortcodes render at priority 8
function enqueue_give_statics( $content ) {
if(preg_match('/class=(\").*?wp-give-form.*?\1/s', $content))
{
Give_Scripts::public_enqueue_scripts();
Give_Scripts::public_enqueue_styles()
}
return $content;
}