Optimize Plugin Performance: Ensuring Unique Execution of Custom Code in WordPress

In my seven-plus years of working extensively with WordPress, I’ve encountered countless scenarios where well-intentioned custom code ends up running multiple times unnecessarily, dragging down site performance. It might not seem like a big deal at first—just a few extra function calls here and there—but over time, and at scale, these inefficiencies can add up and noticeably slow down a site. Fortunately, there are strategies to ensure that your custom plugin logic runs once and only once, resulting in a smoother, more performant WordPress experience.

Understanding the Problem

When you develop custom plugins or add code snippets to functions.php, it’s easy to inadvertently hook the same function multiple times, or run the same initialization logic repeatedly. For instance, you might attach a function to an action hook that also fires on admin pages, Ajax calls, and front-end requests. If not carefully controlled, this can lead to resource-heavy operations (like database queries or external API calls) being executed more than needed.

I’ve learned through experience that controlling execution flow is just as important as writing quality code. It’s not uncommon for clients to come to me with a site suffering sluggish performance, only to discover that a simple plugin was making multiple repetitive calls due to some overlooked logic.

Strategies for Unique Execution

1. Checking for Existing Actions

WordPress provides helpful utilities to check whether an action or filter has already run. For example, did_action() can help you determine if a particular hook has fired, ensuring you don’t run certain logic more than once.

Example:

function my_custom_init_logic() {

    if ( did_action( 'init' ) > 1 ) {

        // 'init' action already fired before, so let's skip

        return;

    }

    // Place your custom code here.

    // This code will run only on the first 'init' execution.

}

add_action( 'init', 'my_custom_init_logic' );

In the example above, my_custom_init_logic() will only execute its main logic the first time init fires, ignoring subsequent runs. This is a simple but effective approach when multiple theme or plugin components trigger the same hook.

2. Using Singleton Patterns for Plugin Classes

In many advanced WordPress plugins, I’ve adopted the singleton pattern. By ensuring there is only one instance of a class in memory, you inherently prevent multiple code executions. This works particularly well for initialization routines, like registering custom post types or loading text domains.

Example Singleton Class:

class My_Unique_Plugin {

    private static $instance = null;

    private function __construct() {

        // Initialization logic

        add_action( 'init', array( $this, 'register_custom_post_types' ) );

    }

    public static function get_instance() {

        if ( null === self::$instance ) {

            self::$instance = new self();

        }

        return self::$instance;

    }

    public function register_custom_post_types() {

        // This code runs only once, regardless of how many times

        // get_instance() is called.

        register_post_type( 'my_custom_type', array(

            'label' => 'My Custom Type',

            'public' => true

        ) );

    }

}

// Load the plugin instance once

My_Unique_Plugin::get_instance();

With this pattern, even if your plugin file is included multiple times (for some reason) or your code is referenced in different contexts, there will only be one instance of your class performing registration logic.

3. Conditional Logic and Global Flags

In some cases, you just need a simple “have I run this yet?” check. While globals are often frowned upon, using a global or static variable as a flag can be a quick and clean solution, especially for small bits of code.

Example Using a Static Flag:

function my_once_only_function() {

    static $has_run = false;

    if ( $has_run ) {

        return; // Already executed, so no need to do it again

    }

    // Place code here that should run only once

    // For instance, loading external resources or generating unique data

    $has_run = true;

}
// Now even if multiple calls are made to this function

// throughout the page load, the logic inside runs just once.

my_once_only_function();

my_once_only_function(); // Will not execute the logic again.

This approach is low-tech but effective. It ensures that no matter how many times the function is called, the core logic will only execute once per page load.

4. Using Transients or Options to Mark Completion

Sometimes, you need to ensure code only runs once ever—or maybe once per day, or once after a certain event. Using transients or options can help mark completion states that persist across page loads.

For example, if you need to run a migration routine only once when a plugin first activates, you can store an option and check for it every time:

Example Using an Option Check:

function my_plugin_activation() {

    // Only run setup if option doesn't exist

    if ( ! get_option( 'my_plugin_setup_done' ) ) {

        // Run heavy database setup logic once

        // ...

        // Mark as done

        update_option( 'my_plugin_setup_done', true );

    }

}

register_activation_hook( __FILE__, 'my_plugin_activation' );

Here, the database setup logic is guaranteed to run only once during activation, and never again unless the option is deleted or changed.

Personal Insight

Over the years, I’ve learned that elegant solutions in WordPress often revolve around understanding the platform’s hooks and lifecycle. Some of my earlier projects, which seemed perfectly coded, ended up facing performance woes because of repeated execution. By anticipating these issues, I’ve been able to write cleaner code from the start. I’ve found it helpful to always ask myself: “Could this block of code run more than once? If so, is that necessary?” If the answer is no, I implement one of these strategies.

Conclusion

Optimizing plugin performance isn’t just about minifying code and caching—it’s also about ensuring that custom logic executes only when needed. By utilizing WordPress’s built-in functions like did_action(), embracing design patterns like the singleton, or employing simple condition checks and persistent options, you can guarantee that your plugin code stays lean, fast, and effective. It might feel like extra work up front, but trust me, the performance dividends are well worth it in the long run.

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *