0

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 <webstower@gmail.com>
 *
 * @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();

}
?>

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

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.

  • Your answer was in the right path.. First, the screen id is edit-wt_whoisservers like I post, and I don´t know why but wordpress tells me that get_current_screen is not a function even though it exist.. so my solution was use the following code: ' add_action ('current_screen', array($this, 'WTScreenId') ); public function WTScreenId( $screen ) { if ( 'edit-wt_whoisservers' == $screen->id ) { .. .. actions and filters to load .. } ' Thanks a lot for the help @qaisar-feroz – Ezequiel Cattaneo Feb 24 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.