WordPress.org

Ready to get started?Download WordPress

Forums

Adminimize
Suggestion with code: Conditional loading of plugin code to improve performance (1 post)

  1. Solinx
    Member
    Posted 1 year ago #

    The "P3 - Plugin Performance Profiler" showed that on pages loaded by visitors who are not logged in, Adminimize comprised around 25% of the total plugin runtime on our blog on every page load.

    This large share is in part explained by the low number of plugins that we run, but even so, if possible a 'heavy' plugin should not load in scenario's where its functionality is known to be unnecessary.

    I used past tense before, because improving the efficiency for the above scenario turned out to be quite simple. For us Adminimize no longer shows up on the charts.

    The solution I used is to move the plugin code from adminimize.php to another php file, and use a function linked to the plugins_loaded action event to include this new file only if the admin bar is being shown.

    Here is the code I used in adminimize.php, including a fallback for older WP versions:

    add_action('plugins_loaded', 'include_adminimize');
    
    function include_adminimize(){
        // WP 3.1.0+
        if( function_exists('is_admin_bar_showing') ) {
            if( is_admin_bar_showing() ){
                include('adminimize_code.php');
            };
        // Fallback
        } else {
            include('adminimize_code.php');
        };
    }

    Of course, loading a plugin using the plugins_loaded event is a bit of a hack. A more elegant solution would have been to add the following bit of code at the top of the plugin file:

    if( function_exists('is_admin_bar_showing') ) {
        if( ! is_admin_bar_showing() ){ return; }
    }

    However, is_admin_bar_showing() depends on is_user_logged_in(), which in turn depends on a few other functions that are all loaded after the plugins are loaded. In short, the elegant solution does not work unless it is made very inelegant.

    While I realize the solution I suggested earlier is not perfect it works fine for me. I'm posting it here because perhaps it can help others as well. And perhaps this initial attempt could lead to a better final solution.

    http://wordpress.org/extend/plugins/adminimize/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic