I have similar code working from within a theme's functions.php file, but I want to move it to a plugin. I've assembled the following but for some reason, this method is having trouble at the AJAX call and always returns zero.

The intention here is that the form will be on the front-end and usable by either logged-in or NOT logged-in users.

Here's my plugin code:

Plugin Name: AAA Hello World
Plugin URI: http://helloworld.com
Description: AJAX response to form submission
Version: 1.0
Author: John Doe
Author URI: http://helloworld.com

class RespondToMyClicks {

  function __construct() {

    add_action( 'wp_enqueue_scripts', array( &$this, 'click_response_styles' ) );
    add_action( 'wp_enqueue_scripts', array( &$this, 'click_response_scripts' ) );

    if( is_admin() ) {
      add_action( 'wp_ajax_the_ajax_hook', array( &$this, 'process_clicky_form' ) );
      add_action( 'wp_ajax_nopriv_the_ajax_hook', array( &$this, 'process_clicky_form' ) );

    add_shortcode( 'click_response_form', array( &$this, 'click_response_form' ) );


  public function click_response_styles() {

    wp_register_style( 'respond-to-my-clicks', plugins_url( dirname( plugin_basename( __FILE__ ) ) . '/styles.css' ) );
    wp_enqueue_style( 'respond-to-my-clicks' );


  public function click_response_scripts() {

    wp_enqueue_script( 'respond-to-my-clicks', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) );
    wp_localize_script( 'respond-to-my-clicks', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );


  public function process_clicky_form() {
    if( !empty( $_POST ) && wp_verify_nonce( $_POST['post_filtration'], 'post_filter_action' ) ) {
      echo "Hello World";

  public function click_response_form() {
    $the_form = '<form id="theForm" method="post" action="">' . 
    wp_nonce_field( 'post_filter_action', 'post_filtration' ) . 
    '<input name="action" type="hidden" value="the_ajax_hook" />
        <label class="check"><input type="checkbox" name="change_me"  value="" onChange="submit_me();" /> Click me for a response</label>
    <div id="response_area"></div>';
    return $the_form;


new RespondToMyClicks();

And here's my Javascript file code:

    function submit_me() {




I've been looking at this too long and have tried different variations of code so hopefully someone can point out the obvious cuz I'm just plain missing it. Thanks!

  • I've tried your code: it works perfectly! Are you sure that your js file is named exactly ajax.js and it exists in the same folder of the plugin file?
    – gmazzap
    Sep 5, 2013 at 14:20
  • Deleting my answer, it's not useful at all. sorry. Agree with @G.M., i've just tested your code and it works.
    – iEmanuele
    Sep 5, 2013 at 14:33
  • Dang! How can that be? Yes G.M. it's getting the ajax.js file okay because I see the entry for it in the head section of the document and clicking the link displays the file so I'm assuming we're good there. I'm going to rip out the similar stuff in my theme to see if that isn't conflicting with it maybe. I STILL get 0 for a response and not Hello World.
    – Suzanne
    Sep 5, 2013 at 18:08
  • Okay... first off thanks for your comments you guys. Every bit of feedback matters, even if it doesn't seem useful. By telling me it DOES work, that simply tells me it's something else in the installation and that WAS helpful. It turns out my caching plugin had cached a previous version of my code and continued to serve that to me when not logged-in GAH.
    – Suzanne
    Sep 5, 2013 at 18:16
  • Hum... can you guys try this while NOT logged-in? The caching issue only fixed this for logged-in users. It's returning the entire document in my response area now for non-logged-in users.
    – Suzanne
    Sep 5, 2013 at 18:35

1 Answer 1


Okay so NOW I have the entire solution. It was two things:

  1. Caching was the first part of the issue. Cleared cache, deactivated caching plugin for the time-being.
  2. The second, less obvious issue was that, for NOT logged-in users, the response from the AJAX call was returning the entire HTML document in the response area. It turns out I had a "redirection" function in my functions.php file that was causing this. It's designed to redirect users below author back to the front-end so they never access the backend and admin-ajax.php technically is operating from the admin side so that was the issue.

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.