• Resolved Yordan Soares

    (@yordansoares)


    Hi guys,

    I created custom notices to different user roles and I’m showing it with the action hook «admin_notice» in Dashboard, that is the reason I decide to remove the «pending_users_notices» fuction from the same action hook to avoid duplicate notices. The action is located inside the construct method of UR_Admin_User_List_Manager class:

    [plugins-directory]/user-registration/includes/admin/class-ur-admin-user-list-manager.php

    I tried with this:

    remove_action( 'admin_notices', array( 'UR_Admin_User_List_Manager', 'pending_users_notices' ) );

    …but don’t works, can you tell me the method to disable it?

    Thank you in advance!

    • This topic was modified 6 years, 8 months ago by Yordan Soares.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @yordansoares,

    Please try wrapping above “remove_action” function inside a hook. Like:

    add_action( 'wp_head', 'remove_ur_action' );
    function remove_ur_action(){
    	remove_action( 'admin_notices', array( 'UR_Admin_User_List_Manager', 'pending_users_notices' ) );
    }

    Regards!

    Thread Starter Yordan Soares

    (@yordansoares)

    Hi again @rumesh38,

    Isn’t wp_head a themes action? I mean, the notices fires on dashboard action ‘admin_notices’, not in frontend. Here is a capture of the message ‘pending_users_notices’ method shows:

    I already tried with:

    // Hook 'admin_notices' with priority 20:
    add_action( 'admin_notices', 'remove_ur_action', 20 );
    // Hook 'admin_notices' with priority  5:
    add_action( 'admin_notices', 'remove_ur_action', 5 );
    // Hook 'admin_notices' with priority 10 (default):
    add_action( 'admin_notices', 'remove_ur_action' );
    // Hook 'init' with priority 20:
    add_action( 'init', 'remove_ur_action', 20 );
    // Hook 'init' with priority  5:
    add_action( 'init', 'remove_ur_action', 5 );
    // Hook 'init' with priority 10 (default):
    add_action( 'init', 'remove_ur_action' );
    function remove_ur_action(){
      remove_action( 'admin_notices', array( 'UR_Admin_User_List_Manager', 'pending_users_notices' ) );
    }

    But still don’t works. In WordPress Developer Resources says “If an action has been added from within a class, for example by a plugin, removing it will require accessing the class variable.” and shows this example code:

    global $my_class;
    remove_action( 'the_content', array( $my_class, 'class_filter_function' ) );

    Does User Registration use a global variable for the class «UR_Admin_User_List_Manager»?

    Hi @yordansoares,

    There is no Global variable for “UR_Admin_User_List_Manager” class in our plugin. Neither the function “pending_users_notices” where you would have used the above code to remove action. We can give you a zip file of our plugin with a temporary solution. Please contact us through our contact form.
    https://wpeverest.com/contact/

    Regards!

    Thread Starter Yordan Soares

    (@yordansoares)

    Thanks for offer a custom fix. I already fixed it commenting the line :33. But this solution did not satisfy me, so I searched for a better way.

    The remove_action core function work very limited with methods from a class. The unique way to remove a method usint it is when the class was instatiate in a global variable. It isn’t the case of The UR_Admin_User_List_Manager class I need to access.

    Browsing the Internet, I found this link where there is a custom function that extends the WP Filter API:

    function remove_class_action($tag, $class = '', $method, $priority = null) : bool {
      global $wp_filter;
    
      if (isset($wp_filter[$tag])) {
        $len = strlen($method);
    
        foreach($wp_filter[$tag] as $_priority => $actions) {
    
          if ($actions) {
            foreach($actions as $function_key => $data) {
    
              if ($data) {
                if (substr($function_key, -$len) == $method) {
    
                  if ($class !== '') {
                    $_class = '';
                    if (is_string($data['function'][0])) {
                      $_class = $data['function'][0];
                    }
                    elseif (is_object($data['function'][0])) {
                      $_class = get_class($data['function'][0]);
                    }
                    else {
                      return false;
                    }
    
                    if ($_class !== '' && $_class == $class) {
                      if (is_numeric($priority)) {
                        if ($_priority == $priority) {
                          //if (isset( $wp_filter->callbacks[$_priority][$function_key])) {}
                          return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                        }
                      }
                      else {
                        return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                      }
                    }
                  }
                  else {
                    if (is_numeric($priority)) {
                      if ($_priority == $priority) {
                        return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                      }
                    }
                    else {
                      return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                    }
                  }
    
                }
              }
            }
          }
        }
    
      }
      return false;
    }

    This function allows you to disable any action or filter inside a class not instantiated. I used this code to fix the issue without worry about overriden by updates:

    add_action('plugins_loaded', function() {    
      remove_class_action('admin_notices', 'UR_Admin_User_List_Manager', 'pending_users_notices');
    });

    Thanks again for your help 😉

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Remove action: pending_user_notices’ is closed to new replies.