Wordpress admin-ajax.php and closures
===

I'm working on a custom post type class, based off of Jeffery Ways Class [https://github.com/JeffreyWay/Easy-WordPress-Custom-Post-Types][1]


In it i'm working on adding an ajax function to remove the current uploaded file. I created to public function to use with closures.

  

    /**
       * Helper method, that attaches a passed function to the 'admin_print_scripts' WP action
       * @param function $cb Passed callback function.
       */
    
    	public function admin_scripts($cb)
    	{
    		add_action("admin_print_scripts", $cb);
    	}
    
    	/**
    	 * Helper method, that attaches a passed function to the 'wp_ajax_my_action' WP action
    	 * @param function $cb Passed callback function.
    	 */
    
    	public function admin_ajax($cb)
    	{
    		add_action("wp_ajax_my_action", $cb);
    	}


then for my fields i'm looping through and on output in the admin i'm calling my closures

    // attach script to listen to to remove click and send data to ajax
    $this->admin_scripts(function() use( $post->ID, $id_name)
    {
    ?>
        <script type="text/javascript" >
        	jQuery(document).ready(function($) {
               
               // setup the data to use to pass to our ajax function
        	   var data = {
        		post: <?php echo $post->ID ?>,
        		field: <?php echo $id_name ?>
        	   };
        
               $('.remove-upload').click(function() {
        		var clicked = $(this),
        		    to_remove = clicked.data('remove');
        
        		    // since 2.8 ajaxurl is always defined in the admin header and points to  admin-ajax.php
        		    $.post(ajaxurl, data, function(response) {
                             $('a[data-remove='+to_remove+'], img[data-remove='+to_remove+']').remove();
        		    });
        
        	    });
        	});
        </script>
    <?php
    } // end admin_scripts closure

the problem i'm running into is that the admin-ajax.php that is used to hook into requires you to have the action to reference so that admin-ajax.php passes the rest of your data to the right wp_ajax_my_action hook.

so in my javascript my var data should really look like this

    var data = {
        action: 'my_action', //this being the name of my function passed to wp_ajax_my_action hook      		
        post: <?php echo $post->ID ?>,
        field: <?php echo $id_name ?>
    };

Does anyone have any recommendations on a better way to handle this so that my ajax calls line up.

this is the page i've been looking at for reference. [http://codex.wordpress.org/AJAX_in_Plugins][2]

Thanks for any advice.


  [1]: https://github.com/JeffreyWay/Easy-WordPress-Custom-Post-Types
  [2]: http://codex.wordpress.org/AJAX_in_Plugins