0

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?

  • 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 Mar 1 at 15:27
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/';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.