I'm developing a plugin and I've started from WPPB and Wordpress 4.3.1. I've named it dogmaweb. I need to submit a form to the plugin via Ajax. The form is created by the plugin itself. In my class-dogmaweb.php I'm trying to add the action that processes the form data server side, and here is what I've come up with, until now:

public function enqueue_scripts()
  $script_handle = 'userprofilesumbit';
  add_action( 'wp_ajax_'.$script_handle, array($this, 'userprofileform_process') );
  add_action( 'wp_ajax_nopriv_'.$script_handle, array($this, 'userprofileform_process') );
  wp_register_script( $script_handle, plugins_url() . '/public/js/dogmaweb-public.js' );
  $userprofilesumbit_data = array('ajax_url' => admin_url( 'admin-ajax.php' ), 'form_action' => $script_handle, 'form_id' => '#'.Dogmaweb::$userprofileform_id);
  wp_localize_script($script_handle, 'submit_params', $userprofilesumbit_data);

The symptom of the problem is that admin-ajax.php is not calling my function and it is returning 0. I've stepped through the wordpress code to understand why (I'm using Netbeans and xdebug). The problem is that these lines of code in plugin.php do not find the $tag in the $wp_filter array. The $tag variable at that point contains "wp_ajax_userprofilesumbit", which is exactly what I specified for the $hook argument to the add_action function (I've used the same $script_handle variable as you can see in my code). I've also stepped through the add_action code and I'm sure the global $wp_filter array does contain "wp_ajax_userprofilesumbit" key when add_action returns after I call it. However I'm also sure that it does not contain that key anymore when plugin.php executes (called by admin-ajax.php).

Why is my filter being removed from $wp_filter? What am I doing wrong?

2 Answers 2


I skimmed through the plugin's code and you could try to use the define_public_hooks() method of the Plugin_Name class to register your ajax action callbacks of the Plugin_Name_Public class:

 * Register all of the hooks related to the public-facing functionality
 * of the plugin.
 * @since    1.0.0
 * @access   private
private function define_public_hooks() {

    $plugin_public = new Plugin_Name_Public( $this->get_plugin_name(), $this->get_version() );

    $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
    $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

    // Add your ajax actions here instead 
    $script_handle = 'userprofilesumbit';   
    $this->loader->add_action(  'wp_ajax_' . $script_handle, $plugin_public, 'userprofileform_process' );
    $this->loader->add_action(  'wp_ajax_nopriv_' . $script_handle, $plugin_public, 'userprofileform_process' );


What is happening there is that you need to move:

add_action( 'wp_ajax_'.$script_handle, array($this, 'userprofileform_process') );
add_action( 'wp_ajax_nopriv_'.$script_handle, array($this, 'userprofileform_process') );

Out of the enqueue_scripts function, because the way it is setup these actions are not been hooked, because this function will only execute on front-end actions.

  • Both your answer and @birgire one are correct and both helped me understand. Yours came first, but his answer is more complete and suggests where to put the add_action calls. I upvoted yours but I'm accepting his answer because of that. Thanks a lot to both of you anyway. Dec 14, 2015 at 9:19
  • Awesome! I'm really glad I was able to help you.
    – Bordoni
    Dec 14, 2015 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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