0

I have a plugin that adds the following action which among other things sends out a registration mail when a new user is added:

add_action('um_post_registration_approved_hook', 'um_post_registration_approved_hook', 10, 2);

I would like to remove this action, so I can add it again with some additional checks in the called function. I have tried the following, but can't get it to work:

add_action( 'um_post_registration_approved_hook', 'remove_my_action', 11 );
function remove_my_action(){
    remove_action('um_post_registration_approved_hook', 'um_post_registration_approved_hook', 10, 2);
}

Any ideas?

EDIT

Turns out I had the priority wrong, thanks! I am now able to remove the action and register a new one instead. However, I'm now having some problems with that. This is what I've unregistered:

add_action('um_post_registration_approved_hook', 'um_post_registration_approved_hook', 10, 2);

    function um_post_registration_approved_hook($user_id, $args){

        global $ultimatemember;

        $ultimatemember->user->approve();

    }

I have then registered the following instead:

add_action( 'um_post_registration_approved_hook', 'remove_my_action', 9 );
function remove_my_action(){
    remove_action('um_post_registration_approved_hook', 'um_post_registration_approved_hook', 10, 2);
    add_action('um_post_registration_approved_hook', 'um_post_registration_approved_hook_new', 10, 2);

}


function um_post_registration_approved_hook_new($user_id, $args){
    newApprove();
}

// Make the action call another version of this function - check for user meta - paupress_pp_user_type

function newApprove(){

    global $ultimatemember;


    $user_id = um_user('ID');

    delete_option( "um_cache_userdata_{$user_id}" );

    if ( um_user('account_status') == 'awaiting_admin_review' ) {

        $this->password_reset_hash();

        $ultimatemember->mail->send( um_user('user_email'), 'approved_email' );


    } else {

        // check for paupress_pp_user_type before sending

        $this->password_reset_hash();
        // DONT SEND THIS MAIL
        //$ultimatemember->mail->send( um_user('user_email'), 'welcome_email');

    }



    $this->set_status('approved');

    $this->delete_meta('account_secret_hash');

    $this->delete_meta('_um_cool_but_hard_to_guess_plain_pw');

    do_action('um_after_user_is_approved', um_user('ID') );

}

Which is simply another version of the original approve()-function, with the mail notification removed. BUT this doesn't work, since the original approve() is defined inside the UM_user class and the function relies on this. The original file is here, with the approve()-function defined from line 941: http://pastebin.com/FknrcxzM

Does my problem make sense? And can I hook into the class instead of the function? - Don't really know the correct approach here..

  • To start to answer your second question, it looks as though the references to this have nowhere to go. If I understand the code here, that isn't the $ultimatemember object? If you change this to the correct object name then that might be sufficient in this case. – Andy Macaulay-Brook Sep 29 '16 at 9:33
  • Thanks. Unfortunately changing this to $ultimatemember doesnt work. It's in the class UM_User { function __construct() { $this->id = 0;, so it might be referring to the UM_User object? – user2806026 Sep 29 '16 at 10:45
  • I'll create a new question. Thanks for your help here :-) – user2806026 Sep 29 '16 at 10:48
  • Cool - you'll probably attract more attention that way! You might want to edit this question back to the shorter version at the same time as that will keep the site format tidy. – Andy Macaulay-Brook Sep 29 '16 at 10:49
3

You want:

remove_action('um_post_registration_approved_hook', 'um_post_registration_approved_hook', 10, 2);

... to run after the original add_action, but before the action triggers the function um_post_registration_approved_hook

The easiest way to do this, but I haven't tested it, might be to just give your removal an earlier priority on the same hook:

add_action( 'um_post_registration_approved_hook', 'remove_my_action', 9 );
function remove_my_action(){
    remove_action('um_post_registration_approved_hook', 'um_post_registration_approved_hook', 10, 2);
}

This is what you were trying, but the priority was the wrong way around. 11 runs after 10.

  • But shouldn't remove_action be run inside a function in order to work? Pretty sure I saw that in the documentation somewhere. – user2806026 Sep 29 '16 at 8:37
  • You only need to ensure it runs after the original add_action and before the action is fired. So you can put it in a function but use a more appropriate hook. What's the context for the add_action? Without that we can't choose a hook for the remove. What fires the um_ action? – Andy Macaulay-Brook Sep 29 '16 at 8:40
  • I'm being slow - I know what you need - one second while I edit the answer. – Andy Macaulay-Brook Sep 29 '16 at 8:41
  • Thanks! I've just confirmed - the first suggestion with just the remove_action line doesn't work in netiher functions.php or another plugin. – user2806026 Sep 29 '16 at 8:53
  • Yeh - I think you just have your priority wrong. Try 9 and not 11. If that fails then we need to use a different hook. – Andy Macaulay-Brook Sep 29 '16 at 8:57

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.