Is_admin()真的提供插件性能改进吗?

时间:2019-02-28 作者:emeraldjava

我的主楼里有这个街区。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()的最佳实践指南?

1 个回复
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/\';
    }
}

相关推荐

当使用HTTPS时,WordPress不会将HTTPS用于css和js,并且admin不起作用。我该怎么解决这个问题?

因此,我最近构建了和LAMP服务器,并在其上安装了wordpress。我使用serveo通过反向ssh隧道将其连接到web。网我决定添加ssl,网站的其他部分似乎运行良好,但当我尝试使用ssl时,wordpress不断崩溃。我转到了常规设置,将URL改为http而不是https,但当我加载页面时,在源代码中css和js URL使用http而不是https,因此它们不会加载。然后wp管理部分甚至不会加载。如何修复此问题?我看过很多关于有类似问题的人的帖子,但没有一篇对我有帮助。