Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active November 22, 2023 22:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save westonruter/af6ce5699e4b5576f51a7cd901b80152 to your computer and use it in GitHub Desktop.
Save westonruter/af6ce5699e4b5576f51a7cd901b80152 to your computer and use it in GitHub Desktop.
<?php
/**
* Contact Form 7 Conditional Enqueues WordPress Plugin.
*
* @package CF7_Conditional_Enqueues
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2023 Google Inc.
*
* @wordpress-plugin
* Plugin Name: Contact Form 7 Conditional Enqueues
* Description: Prevent enqueueing JS and CSS for Contact Form 7 unless there is a form on the page. This is a mini plugin to implement the changes suggested in <a href="https://github.com/rocklobster-in/contact-form-7/issues/1278">contact-form-7#1278</a>.
* Plugin URI: https://gist.github.com/westonruter/af6ce5699e4b5576f51a7cd901b80152
* Version: 0.1.0
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://gist.github.com/westonruter/af6ce5699e4b5576f51a7cd901b80152
*/
namespace CF7_Conditional_Enqueues;
/**
* Required CF6 version.
*
* Version in which the wpcf7_shortcode_callback action was introduced.
*
* @var string
*/
const REQUIRED_CF7_VERSION = '5.8.1';
/**
* Enqueue scripts if not already enqueued.
*/
function conditionally_enqueue_scripts() {
if ( ! did_action( 'wpcf7_enqueue_scripts' ) ) { // Prevent double-enqueueing when multiple forms present.
wpcf7_enqueue_scripts();
}
}
/**
* Enqueue styles if not already enqueued.
*/
function conditionally_enqueue_styles() {
if ( ! did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
wpcf7_enqueue_styles();
}
}
/**
* Add shortcode callbacks to enqueue scripts and styles.
*/
function add_shortcode_callbacks() {
// The wpcf7_shortcode_callback action was added in CF7 version 5.8.1.
if ( ! defined( 'WPCF7_VERSION' ) || version_compare( WPCF7_VERSION, REQUIRED_CF7_VERSION, '<' ) ) {
return;
}
add_filter( 'wpcf7_load_js', '__return_false', PHP_INT_MAX );
add_filter( 'wpcf7_load_css', '__return_false', PHP_INT_MAX );
add_action( 'wpcf7_shortcode_callback', __NAMESPACE__ . '\conditionally_enqueue_scripts' );
add_action( 'wpcf7_shortcode_callback', __NAMESPACE__ . '\conditionally_enqueue_styles' );
}
/*
* The wpcf7_shortcode_callback action fires when the form is rendered. This happens naturally on the frontend but it
* also can happen in the block editor when the post data is obtained from the REST API via
* block_editor_rest_api_preload(). However, the wpcf7_enqueue_scripts() and wpcf7_enqueue_styles() functions are
* not loaded in the admin context. For this reason, callbacks are only added when we're certain to be on the frontend.
*/
add_action( 'template_redirect', __NAMESPACE__ . '\add_shortcode_callbacks' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment