diff --git a/inc/Plugin.php b/inc/Plugin.php index 0c1e6571f5..3f26d85872 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -378,6 +378,7 @@ private function init_common_subscribers() { 'translatepress', 'themify', 'wpgeotargeting', + 'contactform7', 'lazyload_css_subscriber', 'shoptimizer', ]; diff --git a/inc/ThirdParty/Plugins/ContactForm7.php b/inc/ThirdParty/Plugins/ContactForm7.php new file mode 100644 index 0000000000..773c705fa8 --- /dev/null +++ b/inc/ThirdParty/Plugins/ContactForm7.php @@ -0,0 +1,28 @@ + [ 'maybe_optimize_contact_form_7', 10 ], + ]; + } + + /** + * Optimize ContactForm7 scripts. + * + * @return array + */ + public function maybe_optimize_contact_form_7() { + if ( defined( 'WPCF7_LOAD_JS' ) && WPCF7_LOAD_JS && ! has_action( 'wpcf7_shortcode_callback' ) ) { + // Only load the frontend scripts on the new 'wpcf7_shortcode_callback' hook. + add_filter( 'wpcf7_load_js', '__return_false' ); + add_action( 'wpcf7_shortcode_callback', 'wpcf7_enqueue_scripts' ); + } + } +} diff --git a/inc/ThirdParty/ServiceProvider.php b/inc/ThirdParty/ServiceProvider.php index f5c2180c46..f71f946fc1 100644 --- a/inc/ThirdParty/ServiceProvider.php +++ b/inc/ThirdParty/ServiceProvider.php @@ -36,6 +36,7 @@ use WP_Rocket\ThirdParty\Plugins\CDN\{Cloudflare,CloudflareFacade}; use WP_Rocket\ThirdParty\Plugins\Jetpack; use WP_Rocket\ThirdParty\Plugins\WPGeotargeting; +use WP_Rocket\ThirdParty\Plugins\ContactForm7; use WP_Rocket\ThirdParty\Plugins\SEO\RankMathSEO; use WP_Rocket\ThirdParty\Plugins\SEO\AllInOneSEOPack; use WP_Rocket\ThirdParty\Plugins\SEO\SEOPress; @@ -101,6 +102,7 @@ class ServiceProvider extends AbstractServiceProvider { 'rapidload', 'translatepress', 'wpgeotargeting', + 'contactform7' ]; /** @@ -257,5 +259,6 @@ public function register() { ->share( 'rapidload', RapidLoad::class ); $this->getContainer()->share( 'translatepress', TranslatePress::class ); $this->getContainer()->share( 'wpgeotargeting', WPGeotargeting::class ); + $this->getContainer()->share( 'contactform7', ContactForm7::class ); } }