仅为管理员CPT列表加载操作和过滤器

时间:2019-02-23 作者:Ezequiel Cattaneo

我正在开发一个wordpress插件,我有一个自己无法解决的问题。我只在加载CPT管理列表时才尝试加载一些操作和筛选器,我有以下代码:

主插件文件中的代码:

<?php
..
..
..
require_once WTDOMCHECK_PLUGIN_DIR.\'includes/class-plugin.php\';

$WTDOMCHECK_SERVERS = "";
$LICENSE_STATUS = "";

// All the magic is done here!
if ( class_exists(\'plugin\') ) $wtdomcheck = new plugin();

// Activation method
register_activation_hook( __FILE__, array($wtdomcheck, \'WTActivate\' ) );

// Deactivation method
register_deactivation_hook( __FILE__, array($wtdomcheck, \'WTDeactivate\' ) );

// Shortcode to insert the plugin
add_shortcode( \'wtdomcheck\', \'WTDomcheckShow\' );

//Fires basic tasks
$wtdomcheck->WTRegister();


// Where do you want to go?
if ( is_admin() ) {
    require_once( \'admin/wtdomcheck_admin.php\' );
} else {
    require_once( \'public/wtdomcheck_front.php\' );
}
?>
wtdomcheck\\u管理的内容。php:

<?php
/**
 *
 * This file contains the necesary functions to display the admin area of the plugin
 *
 * @author  Ezequiel Cattaneo <[email protected]>
 *
 * @link    https://webstower.com.ar/wtdomcheck
 * @since   1.0.0
 *
 **/

require_once WTDOMCHECK_PLUGIN_DIR.\'admin/class/class-admin.php\';

$wtdomadmin = \'\';

//All the magic is done here!
if ( class_exists(\'admin\') ) {

    $wtdomadmin = new admin();
    $wtdomadmin->WTRegister();

}
?>
以及下面的类文件class admin中的问题。php:

<?
..
..
    //
    // PARAM
    // Register all the actions and filters for the Admin area
    //
    public function WTRegister() 
    {

        $this->settings->
            WTAddPages( $this->pages ) ->
            WTWithSubPage( \'Dashboard\' ) ->
            WTAddSubPages( $this->subpages ) ->
            WTRegister();

        //Load stuffs for the settings page
        add_action( \'load-settings_page_wtdomaincheck\', 
                    array($this, \'WTRegisterSettingsStuff\') 
                  );

        //Load stuffs for the CPT list
        add_action( \'edit-wt_whoisservers\', array($this, \'WTRegisterCPTStuff\') );

        //Load the widget for the dashboard
        add_action( \'wp_dashboard_setup\', 
                    array($this, \'WTDashboardWidgetSettings\') 
                  );

        //PLUGIN LIST: Add custom links to the plugin, below the plugin name
        add_filter( \'plugin_action_links_\'.WTDOMCHECK_PLUGIN_NAME, 
                    array($this->settings, \'WTAddCustomLinks\')
                  );

        // Add to the admin_init action hook
        add_filter(\'current_screen\', array($this, \'WTScreenId\') );
    }
..
..
?>
在这段代码中,只有在屏幕中加载了设置页面,并且功能正常时,才会加载操作和过滤器:

//Load stuffs for the settings page
add_action( \'load-settings_page_wtdomaincheck\', 
            array($this, \'WTRegisterSettingsStuff\') 
          );
问题是,这种代码的和平被忽略,而没有执行:

//Load stuffs for the CPT list
add_action( \'edit-wt_whoisservers\', array($this, \'WTRegisterCPTStuff\') );
I want load actions and filters only when the cpt is displayed at screen.. 获取当前屏幕信息时,获得了屏幕id“edit-wt\\u whoisservers”。

任何帮助都将不胜感激!提前感谢

1 个回复
最合适的回答,由SO网友:Qaisar Feroz 整理而成

问题是,这种代码的和平被忽略,而没有执行:

这是因为没有action 使用名称edit-wt_whoisservers

从你的代码来看WTRegisterCPTStuff() 通过add_action, 您可以直接调用它来加载CPT列表的资源。

因此,以下代码

//Load stuffs for the CPT list
add_action( \'edit-wt_whoisservers\', array($this, \'WTRegisterCPTStuff\') );
可以替换为以下内容:

if ( \'wt_whoisservers\' == get_current_screen()->id ) {    // 
    $this->WTRegisterCPTStuff();
 }
我希望这有帮助。

相关推荐

wp_list_tables bulk actions

如何在扩展的WP\\U List\\U表中触发批量操作。我一直在将以下批量操作添加到may table的选择框中,但在Apply上不会发生任何事情下面是我如何添加批量操作的function get_bulk_actions() { $actions = array( \'delete\' => \'Delete\', \'parsing\' => \'Parsen\' );