Load actions and filters only for the admin CPT list

admin2025-06-02  3

I'm developing a wordpress plugin, and I have a trouble that I can´t resolve for myself. Im trying to load some actions and filters only when the CPT admin list was loaded I have the following code:

The code in main plugin file:

<?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' );
}
?>

The content of the wtdomcheck_admin.php:

<?php
/**
 *
 * This file contains the necesary functions to display the admin area of the plugin
 *
 * @author  Ezequiel Cattaneo <[email protected]>
 *
 * @link    
 * @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();

}
?>

And the trouble in the following class file 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') );
    }
..
..
?>

In this peace of code, the actions and filters only load if the settings page is loaded in the screen, and function right ok:

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

The trouble is that this peace of code are ignored and not executed:

//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.. the screen id "edit-wt_whoisservers" was obtained getting the current screen info.

Any help will be apreciated! Thanks in advance

I'm developing a wordpress plugin, and I have a trouble that I can´t resolve for myself. Im trying to load some actions and filters only when the CPT admin list was loaded I have the following code:

The code in main plugin file:

<?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' );
}
?>

The content of the wtdomcheck_admin.php:

<?php
/**
 *
 * This file contains the necesary functions to display the admin area of the plugin
 *
 * @author  Ezequiel Cattaneo <[email protected]>
 *
 * @link    https://webstower.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();

}
?>

And the trouble in the following class file 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') );
    }
..
..
?>

In this peace of code, the actions and filters only load if the settings page is loaded in the screen, and function right ok:

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

The trouble is that this peace of code are ignored and not executed:

//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.. the screen id "edit-wt_whoisservers" was obtained getting the current screen info.

Any help will be apreciated! Thanks in advance

Share Improve this question edited Feb 23, 2019 at 14:33 Ezequiel Cattaneo asked Feb 23, 2019 at 14:26 Ezequiel CattaneoEzequiel Cattaneo 256 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The trouble is that this peace of code are ignored and not executed:

It is because there is no action with name edit-wt_whoisservers

From your code it seems that instead of calling WTRegisterCPTStuff() through an add_action, you can call it directly to load resources for the CPT list.

So, the following code

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

can be replaced with something like this:

if ( 'wt_whoisservers' == get_current_screen()->id ) {    // 
    $this->WTRegisterCPTStuff();
 }

I hope this helps.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748872990a314427.html

最新回复(0)