There is a plugin that is loading ajax actions like so:

add_action( 'wp_ajax_nopriv_func_name', array( &$this, 'func_name' ) );
add_action( 'wp_ajax_get_func_name', array( &$this, 'func_name' ) );

I want to remove them in my theme so I am doing:

remove_all_actions('wp_ajax_nopriv_func_name');
remove_all_actions('wp_ajax_get_func_name');

But they are still there. I am definitely doing this after they have been added. I brought $wp_filter into scope and saw they were definitely set before my code and unset after.

It looks though like wordpress when you add ajax actions works a little differently. I think the actual action is stored somewhere else and I am just removing an alias to it as when I look inside the $wp_filter array the function name is like 4823746378642374682746func_name.

I am not sure where it is referencing or what the procedure is for removing it.

Can anyone shed some light on this for me?

Edit:

I think the numbers are because the class object is being passed, I guess its a hash of the class.

Update:

This appears to be a bug/inconsistency. I did the following test:

    add_action('wp_ajax_nopriv_ajax_test', array(&$this, 'ajax_test'));
    add_action('wp_ajax_ajax_test', array(&$this, 'ajax_test'));
    remove_all_actions('wp_ajax_nopriv_ajax_test');
    remove_all_actions('wp_ajax_ajax_test');

the ajax_test function is still accessible from javascript even though it was removed immediately after adding.

Does anyone know where this is being stored?

Update:

I found the code responsible in admin-ajax.php

do_action( 'wp_ajax_' . $_REQUEST['action'] );

The really weird thing I can't get my head around is that the ajax request returns before this code is hit. So it seems this code is just for doing associated actions and not returning the ajax request.

  • What hook are you using to remove the action? It could be that you're hook isn't loaded when the AJAX is called. – Manny Fleurmond May 18 '15 at 1:06
  • Yeah, it was a combination of this and a missing wp_die() confusing things. – Guerrilla May 18 '15 at 1:24

The main issue I was having is that when an ajax request hits not all hooks are being loaded.

I used this code:

$return = array();
foreach( $GLOBALS['wp_actions'] as $action => $count )
{
    $return["message"] .= $action . ", ";
}
wp_send_json($return);

This showed me the hooks that were running before and I got it to work.

I think removing ajax actions on "wp_loaded" is probably best as it seems to be the last hook before admin ajax is hit. If you remove on "admin_init" then you miss the check at the top of the file although that may mean you miss some so maybe it is better to do on admin_init.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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