Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active November 22, 2023 22:09
Show Gist options
  • Select an option

  • Save westonruter/af6ce5699e4b5576f51a7cd901b80152 to your computer and use it in GitHub Desktop.

Select an option

Save westonruter/af6ce5699e4b5576f51a7cd901b80152 to your computer and use it in GitHub Desktop.

Revisions

  1. westonruter revised this gist Oct 10, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion contact-form-7-conditional-enqueues.php
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ function conditionally_enqueue_scripts() {
    * Enqueue styles if not already enqueued.
    */
    function conditionally_enqueue_styles() {
    if ( ! did_action( 'wpcf7_enqueue_styles' ) ) {
    if ( ! did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
    wpcf7_enqueue_styles();
    }
    }
  2. westonruter revised this gist Oct 9, 2023. 1 changed file with 16 additions and 2 deletions.
    18 changes: 16 additions & 2 deletions contact-form-7-conditional-enqueues.php
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,14 @@

    namespace CF7_Conditional_Enqueues;

    add_filter( 'wpcf7_load_js', '__return_false', PHP_INT_MAX );
    add_filter( 'wpcf7_load_css', '__return_false', PHP_INT_MAX );
    /**
    * 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.
    @@ -46,6 +52,14 @@ function conditionally_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' );
    }
  3. westonruter revised this gist Oct 9, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion contact-form-7-conditional-enqueues.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    /**
    * Contact Form 7 Conditional Enqueues WordPress Plugin.
    *
    * @package OptimizeContentHeroImage
    * @package CF7_Conditional_Enqueues
    * @author Weston Ruter, Google
    * @license GPL-2.0-or-later
    * @copyright 2023 Google Inc.
  4. westonruter revised this gist Oct 9, 2023. 1 changed file with 21 additions and 18 deletions.
    39 changes: 21 additions & 18 deletions contact-form-7-conditional-enqueues.php
    Original file line number Diff line number Diff line change
    @@ -21,36 +21,39 @@

    namespace CF7_Conditional_Enqueues;

    add_filter( 'wpcf7_load_js', '__return_false' );
    add_filter( 'wpcf7_load_css', '__return_false' );
    add_filter( 'wpcf7_load_js', '__return_false', PHP_INT_MAX );
    add_filter( 'wpcf7_load_css', '__return_false', PHP_INT_MAX );

    /**
    * Conditionally enqueue scripts.
    * Enqueue scripts if not already enqueued.
    */
    function conditionally_enqueue_scripts() {
    if (
    function_exists( 'wpcf7_enqueue_scripts' ) && // Does not exist in admin context.
    ! did_action( 'wpcf7_enqueue_scripts' )
    ) {
    if ( ! did_action( 'wpcf7_enqueue_scripts' ) ) { // Prevent double-enqueueing when multiple forms present.
    wpcf7_enqueue_scripts();
    }

    }

    /**
    * Conditionally enqueue styles.
    * Enqueue styles if not already enqueued.
    */
    function conditionally_enqueue_styles() {
    if (
    function_exists( 'wpcf7_enqueue_styles' ) && // Does not exist in admin context.
    ! did_action( 'wpcf7_enqueue_styles' )
    ) {
    if ( ! did_action( 'wpcf7_enqueue_styles' ) ) {
    wpcf7_enqueue_styles();
    }
    }

    // Note: The wpcf7_shortcode_callback action fires 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. This necessitates the function_exists() checks.
    add_action( 'wpcf7_shortcode_callback', __NAMESPACE__ . '\conditionally_enqueue_scripts' );
    add_action( 'wpcf7_shortcode_callback', __NAMESPACE__ . '\conditionally_enqueue_styles' );
    /**
    * Add shortcode callbacks to enqueue scripts and styles.
    */
    function add_shortcode_callbacks() {
    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' );
  5. westonruter revised this gist Oct 9, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions contact-form-7-conditional-enqueues.php
    Original file line number Diff line number Diff line change
    @@ -10,13 +10,13 @@
    * @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: ...
    * 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: ...
    * Update URI: https://gist.github.com/westonruter/af6ce5699e4b5576f51a7cd901b80152
    */

    namespace CF7_Conditional_Enqueues;
  6. westonruter created this gist Oct 9, 2023.
    56 changes: 56 additions & 0 deletions contact-form-7-conditional-enqueues.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    <?php
    /**
    * Contact Form 7 Conditional Enqueues WordPress Plugin.
    *
    * @package OptimizeContentHeroImage
    * @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: ...
    * 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: ...
    */

    namespace CF7_Conditional_Enqueues;

    add_filter( 'wpcf7_load_js', '__return_false' );
    add_filter( 'wpcf7_load_css', '__return_false' );

    /**
    * Conditionally enqueue scripts.
    */
    function conditionally_enqueue_scripts() {
    if (
    function_exists( 'wpcf7_enqueue_scripts' ) && // Does not exist in admin context.
    ! did_action( 'wpcf7_enqueue_scripts' )
    ) {
    wpcf7_enqueue_scripts();
    }

    }

    /**
    * Conditionally enqueue styles.
    */
    function conditionally_enqueue_styles() {
    if (
    function_exists( 'wpcf7_enqueue_styles' ) && // Does not exist in admin context.
    ! did_action( 'wpcf7_enqueue_styles' )
    ) {
    wpcf7_enqueue_styles();
    }
    }

    // Note: The wpcf7_shortcode_callback action fires 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. This necessitates the function_exists() checks.
    add_action( 'wpcf7_shortcode_callback', __NAMESPACE__ . '\conditionally_enqueue_scripts' );
    add_action( 'wpcf7_shortcode_callback', __NAMESPACE__ . '\conditionally_enqueue_styles' );