I’ve created a fairly simple class for handing the creation of metaboxes within wordpress admin.
I’ve set up the class so that each instance of the class represents an new metabox definition. Each class instance stores its metabox definition as a private class variable, which is used throughout the class for drawing the metabox fields etc. So far, so good. There is a separate class variable which defines if the metabox is repeatable. If it is, the metabox class adds in the necessary actions and javascript to handle the admin ajax call.
Now, this call to admin ajax is causing an odd issue. For some reason, the admin ajax call always returns the metabox definition of the first created metabox, regardless of what metabox is actually calling the admin_ajax method.
This is how I set up the admin ajax action. The conditional just checks if the metabox supports repeatable elements.
if($this->is_dynamic){
add_action('wp_ajax_get_cloned_content', array(&$this,'ref_AJAX_get_cloned_content'));
}
Below is my admin ajax call.
public function ref_AJAX_get_cloned_content( ){
check_ajax_referer('get_cloned_content', 'nonce');
$newFormData = json_encode($this->renderForm($this->m_box['fields']));
$success_response = new WP_Ajax_Response();
$success_response->add(array(
'what' => 'mbox',
'data' => $newFormData,
'supplemental' => array('fielddata' => $newFormData)
));
$success_response->send();
exit;
}
And this is the javascript call to the admin ajax handler.
var data = {
action: 'get_cloned_content',
nonce: <?php echo wp_create_nonce('get_cloned_content');?>,
};
$.post(ajaxurl, data, function(response) {
var res = wpAjax.parseAjaxResponse(response, 'ajax-response');
jQuery.each( res.responses, function() {
console.log(this.supplemental.fielddata);
});
});
There are no problems calling the ajax method and getting a response, its just getting the actual class instances field data back that is the issue. I don’t know why wordpress wont return my class instances variable values from that admin ajax call.
Any ideas on why this would be occurring?
I can get around it by using a different non admin ajax method to clone the metabox fields, but I’d still like to know why the issue I have is occurring, after banging my head on the wall with it.