-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditionally enqueue scripts and styles only if there is a contact form rendered #1279
Conditionally enqueue scripts and styles only if there is a contact form rendered #1279
Conversation
*/ | ||
add_action( | ||
'wp_enqueue_scripts', | ||
static function () { | ||
'wp_default_scripts', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the use of wp_default_scripts
is needed due to block themes since they apply the_content
filters before the wp_enqueue_scripts
action runs. By using wp_default_scripts
and wp_default_styles
we can be assured that the scripts and styles are registered so that they can be enqueued when blocks are rendered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it will have a major impact for the performance of sites using CF7 🎉
+1 from my end!
|
||
if ( wpcf7_load_css() ) { | ||
wpcf7_enqueue_styles(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that these styles only affect the response related markup that is only triggered by an AJAX submission, it should be fine to load the CSS later - i.e. no layout shifts 👍
18600a2
to
9d8fee2
Compare
This improves frontend performance (the LCP metric) of the plugin by eliminating stylesheets and scripts from being enqueued on the frontend if there are no contact forms present. When all integrations are enabled (Stripe and
reCAPTCHA) this reduces TTFB-LCP by almost half on vanilla WordPress homepages (running Twenty Twenty-Three) without contact forms.Update regarding reCAPTCHA: As pointed out by @PhilMakower in #1278 (comment), reCAPTCHA actually works best when it is included on every page, even those that don't use contact forms. So when the reCAPTCHA integration is enabled, its script will continue to be enqueued on all pages. Once this PR is merged, I'll open another one to implement async loading of the reCAPTCHA scripts (
add/async-recaptcha
branch)Fixes #1278.