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?
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 ofadmin\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.