contact-form-7#1278. * 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' );