我的主楼里有这个街区。php类,该类加载构成插件的admin、core和front类
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);
}
我可以使用
is_admin() 函数来限制像这样加载管理控制器类
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);
}
既然我的类注册的操作和过滤器已经特定于wordpress站点的公共或管理区域,那么这里真正添加的是什么值?正如我所看到的,使用该方法可以避免注册一些钩子方法,这些方法无论如何都不会被调用。
我有一些CPT类,这些类在公共和管理站点上使用。这里是否有使用is\\u admin()的最佳实践指南?
SO网友:John Zenith
这个is_admin()
函数主要用于检查正在运行的请求是否在/wp admin/上路由。调用is\\u admin()条件函数,可以限制仅当请求通过/wp admin/时才要加载的文件。
请注意,is\\u admin()和is\\u super\\u admin()不一样,它们处理不同的条件。
展望未来,当发送ajax请求时,它将通过/wp admin/folder,因此最好执行以下操作:
if ( is_admin() || wp_doing_ajax() ) {
// code here
}
谈到最佳使用,我想说的是优化、效率和速度。加载整个
class files
立即执行以下操作
if ( is_admin() ) { ... }
从来都不是一个好的做法。您还应该将检查扩展到需要条件的独立操作。例如:
if ( is_admin() ) {
require_once \'/path/to/file/\';
if ( $_GET[\'type\'] ?? false ) {
include_once \'/path/to/another/file/\';
}
}