admin - Does is_admin() really provide a plugin performance improvement?

admin2025-06-02  1

I have this block in my Main.php class which loads the admin, core and front classes that make up my plugin

function registerObjects() {
    // register the front
    $controller = new front\Controller( $this->get_plugin_name(), $this->get_version());
    $controller->registerHooks($this->loader);

    // register the BHAA objects with support actions and filters.
    $adminController = new admin\AdminController($this->get_plugin_name(), $this->get_version());
    $adminController->registerHooks($this->loader);
    $runnerAdminController = new admin\RunnerAdminController();
    $runnerAdminController->registerHooks($this->loader);
    $raceAdminController = new admin\RaceAdminController();
    $raceAdminController->registerHooks($this->loader);
    $leagueAdminController = new admin\LeagueAdminController();
    $leagueAdminController->registerHooks($this->loader);
    $registrarAdminController = new admin\RegistrarAdminController();
    $registrarAdminController->registerHooks($this->loader);

    // register the core objects
    $raceCpt = new core\cpt\RaceCPT();
    $raceCpt->registerHooks($this->loader);
    $houseCpt = new core\cpt\HouseCPT();
    $houseCpt->registerHooks($this->loader);
    $leagueCpt = new core\cpt\LeagueCPT();
    $leagueCpt->registerHooks($this->loader);
    $connections = new core\Connections();
    $connections->registerHooks($this->loader);
    $eventExpresso = new core\eventexpresso\EventExpresso();
    $eventExpresso->registerHooks($this->loader);

    new core\standard\StandardShortcode();
    new core\race\RaceShortcode();
    new core\league\LeagueShortcode();
    new core\runner\RunnerShortcode();
    $resultsShortcode = new core\results\ResultsShortcode();
    $resultsShortcode->registerHooks($this->loader);
}

I can use the is_admin() function to restrict loading of the admin controller classes like this

function registerObjects() {
    // register the front
    $controller = new front\Controller( $this->get_plugin_name(), $this->get_version());
    $controller->registerHooks($this->loader);

    // register the BHAA objects with support actions and filters.
    if(is_admin()) {
        $adminController = new admin\AdminController($this->get_plugin_name(), $this->get_version());
        $adminController->registerHooks($this->loader);
        $runnerAdminController = new admin\RunnerAdminController();
        $runnerAdminController->registerHooks($this->loader);
        $raceAdminController = new admin\RaceAdminController();
        $raceAdminController->registerHooks($this->loader);
        $leagueAdminController = new admin\LeagueAdminController();
        $leagueAdminController->registerHooks($this->loader);
        $registrarAdminController = new admin\RegistrarAdminController();
        $registrarAdminController->registerHooks($this->loader);
    }

    // register the core objects
    $raceCpt = new core\cpt\RaceCPT();
    $raceCpt->registerHooks($this->loader);
    $houseCpt = new core\cpt\HouseCPT();
    $houseCpt->registerHooks($this->loader);
    $leagueCpt = new core\cpt\LeagueCPT();
    $leagueCpt->registerHooks($this->loader);
    $connections = new core\Connections();
    $connections->registerHooks($this->loader);
    $eventExpresso = new core\eventexpresso\EventExpresso();
    $eventExpresso->registerHooks($this->loader);

    new core\standard\StandardShortcode();
    new core\race\RaceShortcode();
    new core\league\LeagueShortcode();
    new core\runner\RunnerShortcode();
    $resultsShortcode = new core\results\ResultsShortcode();
    $resultsShortcode->registerHooks($this->loader);
}

Since the actions and filter that my classes register are already specific to the public or admin area's of the wordpress site - what value is the is_admin() function really adding here? As i see using the method avoid registering some hook methods, which wouldn't get called anyway.

I have a number of CPT classes which are used on the public and admin site. Are there any best practice guidelines for using is_admin() here?

I have this block in my Main.php class which loads the admin, core and front classes that make up my plugin

function registerObjects() {
    // register the front
    $controller = new front\Controller( $this->get_plugin_name(), $this->get_version());
    $controller->registerHooks($this->loader);

    // register the BHAA objects with support actions and filters.
    $adminController = new admin\AdminController($this->get_plugin_name(), $this->get_version());
    $adminController->registerHooks($this->loader);
    $runnerAdminController = new admin\RunnerAdminController();
    $runnerAdminController->registerHooks($this->loader);
    $raceAdminController = new admin\RaceAdminController();
    $raceAdminController->registerHooks($this->loader);
    $leagueAdminController = new admin\LeagueAdminController();
    $leagueAdminController->registerHooks($this->loader);
    $registrarAdminController = new admin\RegistrarAdminController();
    $registrarAdminController->registerHooks($this->loader);

    // register the core objects
    $raceCpt = new core\cpt\RaceCPT();
    $raceCpt->registerHooks($this->loader);
    $houseCpt = new core\cpt\HouseCPT();
    $houseCpt->registerHooks($this->loader);
    $leagueCpt = new core\cpt\LeagueCPT();
    $leagueCpt->registerHooks($this->loader);
    $connections = new core\Connections();
    $connections->registerHooks($this->loader);
    $eventExpresso = new core\eventexpresso\EventExpresso();
    $eventExpresso->registerHooks($this->loader);

    new core\standard\StandardShortcode();
    new core\race\RaceShortcode();
    new core\league\LeagueShortcode();
    new core\runner\RunnerShortcode();
    $resultsShortcode = new core\results\ResultsShortcode();
    $resultsShortcode->registerHooks($this->loader);
}

I can use the is_admin() function to restrict loading of the admin controller classes like this

function registerObjects() {
    // register the front
    $controller = new front\Controller( $this->get_plugin_name(), $this->get_version());
    $controller->registerHooks($this->loader);

    // register the BHAA objects with support actions and filters.
    if(is_admin()) {
        $adminController = new admin\AdminController($this->get_plugin_name(), $this->get_version());
        $adminController->registerHooks($this->loader);
        $runnerAdminController = new admin\RunnerAdminController();
        $runnerAdminController->registerHooks($this->loader);
        $raceAdminController = new admin\RaceAdminController();
        $raceAdminController->registerHooks($this->loader);
        $leagueAdminController = new admin\LeagueAdminController();
        $leagueAdminController->registerHooks($this->loader);
        $registrarAdminController = new admin\RegistrarAdminController();
        $registrarAdminController->registerHooks($this->loader);
    }

    // register the core objects
    $raceCpt = new core\cpt\RaceCPT();
    $raceCpt->registerHooks($this->loader);
    $houseCpt = new core\cpt\HouseCPT();
    $houseCpt->registerHooks($this->loader);
    $leagueCpt = new core\cpt\LeagueCPT();
    $leagueCpt->registerHooks($this->loader);
    $connections = new core\Connections();
    $connections->registerHooks($this->loader);
    $eventExpresso = new core\eventexpresso\EventExpresso();
    $eventExpresso->registerHooks($this->loader);

    new core\standard\StandardShortcode();
    new core\race\RaceShortcode();
    new core\league\LeagueShortcode();
    new core\runner\RunnerShortcode();
    $resultsShortcode = new core\results\ResultsShortcode();
    $resultsShortcode->registerHooks($this->loader);
}

Since the actions and filter that my classes register are already specific to the public or admin area's of the wordpress site - what value is the is_admin() function really adding here? As i see using the method avoid registering some hook methods, which wouldn't get called anyway.

I have a number of CPT classes which are used on the public and admin site. Are there any best practice guidelines for using is_admin() here?

Share Improve this question asked Feb 28, 2019 at 22:17 emeraldjavaemeraldjava 2813 silver badges16 bronze badges 1
  • 1 John's answer is pretty informative. You should only load what you need, when you need it. The is_admin check is a good start, but as John noted, it's good to also do more specific checks for instance if you don't need an instance of admin\RunnerAdminController on every admin page. That said, there is a performance benefit to not loading admin classes on the front-end, and a bonus advantage that the less you load, the less that can potentially break during a front-end execution. – phatskat Commented Mar 1, 2019 at 15:27
Add a comment  | 

1 Answer 1

Reset to default 1

The is_admin() function is basically used to check whether the running request is routing on /wp-admin/. Calling the is_admin() conditional function, you can restrict files you only want to load when a request goes through the /wp-admin/.

Note that is_admin() and is_super_admin() are not the same and both handle different conditions.

Looking forward, when sending an ajax request, it goes through the /wp-admin/ folder, so it would be nice to do:

if ( is_admin() || wp_doing_ajax() ) {
    // code here
}

Talking about best usage, I would say optimization, efficiency, and speed. Loading the entire class files at once by doing if ( is_admin() ) { ... } is never a good practice. You should also extend the check to an independent action that requires a condition. For example:

if ( is_admin() ) {
     require_once '/path/to/file/';
    if ( $_GET['type'] ?? false ) {
        include_once '/path/to/another/file/';
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748853513a314267.html

最新回复(0)