• Resolved Duncan

    (@duncan1967)


    Issue

    When enabled, WP Optimize appears to be the cause of other plugins loading twice – even when all its settings are disabled, and saved. This happens with all other plugins disabled (except HeartBeat Control, to rule out heartbeat AJAX calls loading a page). Observed under both Apache and NginX on Windows, with different PHP versions.

    The environment is local development and no web-accessible page is available.

    Details

    Clean test environment using Laragon in Windows.

    Simple test plugin My Plugin was created with a single callback to enqueue scripts, code below. A separate MU logging function loggit() is used to log what’s happening.

    Logging takes place:

    – when the plugin is called
    – within the wp_enqueue_scripts callback

    CSS and JS dummy files are loaded, containing only a single comment line. Here is the My Plugin code.

    <?php is the My Plugin cde
    /**
    * Plugin Name: My Plugin
    * Description: A simple plugin that enqueues CSS and JS files.
    * Version: 1.0
    * Author: Your Name
    */

    // Prevent direct access
    if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
    }

    loggit('-- My Plugin called');

    // Enqueue CSS and JavaScript files
    function my_plugin_enqueue_scripts() {
    loggit(' -- -- My Plugin: my_plugin_enqueue_scripts() called.');

    // Enqueue CSS
    wp_enqueue_style( 'my-plugin-style', plugin_dir_url( __FILE__ ) . 'css/my-plugin.css' );

    // Enqueue JavaScript
    wp_enqueue_script( 'my-plugin-script', plugin_dir_url( __FILE__ ) . 'js/my-plugin.js', array( 'jquery' ), '1.0', true );
    }

    // Hook to enqueue scripts
    add_action( 'wp_enqueue_scripts', 'my_plugin_enqueue_scripts' );

    Tests with Apache (Windows)

    Environment:

    WordPress under Laragon (Windows 10, 64 bit)
    Laragon has: MySQL 8.4.0-win64, Apache 2.4.35-win64 with PHP 7.4.5-win32

    Theme:
    Twenty Twenty-three (no child theme)

    Plugins active:
    – Heartbeat Control by WP Rocket (with heartbeat disabled everywhere)
    – My Plugin

    WP Optimize 3.8.0 enabled

    Chrome cache cleared, Apache restarted.
    WordPress Sample page loaded => My Plugin is called three times 🙁

    12/12/2024 13:39:14 Function include_once()  -- My Plugin called 
    12/12/2024 13:39:14 Function include_once()  -- My Plugin called 
    12/12/2024 13:39:14 Function my_plugin_enqueue_scripts()  -- -- My Plugin: my_plugin_enqueue_scripts() called. 
    12/12/2024 13:39:15 Function include_once() -- My Plugin called 
    12/12/2024 13:39:15 Function my_plugin_enqueue_scripts()  -- -- My Plugin: my_plugin_enqueue_scripts() called. 

    WP Optimize 3.8.0 disabled

    Chrome cache cleared, Apache restarted.
    Sample page loaded (shift+reload in Chrome) => simple My Plugin called once. 🙂

    12/12/2024 13:42:38 Function include_once() -- My Plugin called 
    12/12/2024 13:42:38 Function my_plugin_enqueue_scripts()  -- -- My Plugin: my_plugin_enqueue_scripts() called. 

    Repeating the Tests with Nginx Windows

    Repeat test with Apache => Nginx and a later PHP.
    Laragon now has: MySQL 8.4.0-win64, Nginx 1.27.1 win, PHP 8.3.4-win32

    Chrome cache cleared, NginX restarted.
    Sample page loaded (shift+reload in Chrome)
    Results:
    WP Optimize 3.8.0 disabled => My Plugin runs once, always 🙂
    WP Optimize 3.8.0 enabled => My Plugin runs twice, always 🙁

    I absolutely love the database optimisation of the plugin (thank you!) which is the sole use I make of it in development, but am at a loss to explain why it appears to cause multiple loads of other plugins.

    Your help is appreciated, thank you. 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support vupdraft

    (@vupdraft)

    I would enqueue them individually, try something like this

    function my_plugin_scripts() {

      wp_register_script( ‘my-plugin-script’, ‘http://example.com/path/to/my-plugin-script.js&#8217;, array(‘jquery’), ‘1.0’, true );

      wp_enqueue_script( ‘my-plugin-script’ );

    }

    add_action( ‘wp_enqueue_scripts’, ‘my_plugin_scripts’ );

    function my_plugin_styles() {

      wp_register_style( ‘my-plugin-style’, ‘http://example.com/path/to/my-plugin-style.css&#8217;, array(), ‘1.0’, ‘all’ );

      wp_enqueue_style( ‘my-plugin-style’ );

    }

    add_action( ‘wp_enqueue_scripts’, ‘my_plugin_styles’ );

    Thread Starter Duncan

    (@duncan1967)

    Many thanks for the feedback – though that wasn’t the cause (see below, issue resolved). To explain, I had actually moved the CSS/JS enqueues to a single callback as part of debugging and have now reverted to separate callbacks, as is the usual practice.

    I must apologise, despite the issue arising only when WP Optimize was activated this plugin seems not to be the underlying cause.

    Reverting to the theme and child theme I actually use, today, and repeating the tests, finds My Plugin is loaded twice (irrespective of single/dual enqueue callbacks), as before.

    However, the actual environment I use includes ACF. When ACF is not also enabled – so just HeartBeat Control, WP Optimize – this, today, triggered a PHP error. Naturally, ACF was then enabled.

    Enabling Heartbeat Control, WP Optimize *and* now ACF results in a single call to My Plugin, as desired (yipee!).

    This suggests the test environment may not have been as pristine/isolated as hoped for, despite no PHP errors being evident in earlier tests.. Whatever is or was the cause, WP Optimize is not the cause of the issue. 🙂

    Again, many thanks for your time and feedback, it is really much appreciated.

    Best wishes from Hong Kong,

    Duncan.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Other plugins called twice when WP Optimize enabled’ is closed to new replies.