Skip to content

Commit

Permalink
Fix contact form 7 enqueuing scripts issues (#6341)
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan authored Dec 22, 2023
1 parent 610a225 commit 0484f52
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions inc/ThirdParty/Plugins/ContactForm7.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ContactForm7 implements Subscriber_Interface {
*/
public static function get_subscribed_events() {
return [
'template_redirect' => [ 'maybe_optimize_contact_form_7', 10 ],
'template_redirect' => 'maybe_optimize_contact_form_7',
];
}

Expand All @@ -33,8 +33,8 @@ public function maybe_optimize_contact_form_7() {
}

// Force scripts and styles to not load by default.
add_filter( 'wpcf7_load_js', '__return_false', PHP_INT_MAX );
add_filter( 'wpcf7_load_css', '__return_false', PHP_INT_MAX );
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );

// Conditionally enqueue scripts.
add_action( 'wpcf7_shortcode_callback', [ $this, 'conditionally_enqueue_scripts' ] );
Expand All @@ -45,17 +45,27 @@ public function maybe_optimize_contact_form_7() {
* Enqueue scripts if not already enqueued.
*/
public function conditionally_enqueue_scripts() {
if ( ! did_action( 'wpcf7_enqueue_scripts' ) ) { // Prevent double-enqueueing when multiple forms present.
if ( did_action( 'wpcf7_enqueue_scripts' ) ) { // Prevent double-enqueueing when multiple forms present.
return;
}
if ( did_action( 'wp_enqueue_scripts' ) ) {
wpcf7_enqueue_scripts();
return;
}
add_filter( 'wpcf7_load_js', '__return_true', 11 );
}

/**
* Enqueue styles if not already enqueued.
*/
public function conditionally_enqueue_styles() {
if ( ! did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
if ( did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
return;
}
if ( did_action( 'wp_enqueue_scripts' ) ) {
wpcf7_enqueue_styles();
return;
}
add_filter( 'wpcf7_load_css', '__return_true', 11 );
}
}

0 comments on commit 0484f52

Please sign in to comment.