Metabox cloning via Admin Ajax call issue

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.

Read More

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.

Related posts

Leave a Reply

1 comment

  1. 1st This isn’t a WP problem, but a jQuery problem.

    2nd I used to develop my own set of classes for meta box and stopped about a month a ago and dropped them in favor of the RW Meta Box library. Meta Boxes are a big issue incl. saving the data, building an endless # of field types, etc. so that it’s just easier to throw some time together and make something bigger. Currently we got 21 forks. I’d highly encourage you to jump right in. We even got clone fields and aren’t too far away from clone fieldsets.

    3rd Your problem is each();. This doesn’t work with objects that aren’t created after DOM was ready (you can’t attach anything to something that doesn’t exist at this moment, right?). You could try to work with live();, but would encounter serious memory problems. Before jQuery 1.7, you should’ve used delegate(); and should now go with on();. Just take a look at the js file clones.js in the RW Meta Box library for a how-to if you still want to develop your own.


    P.s.: Fork is ♥ – I’d really appreciate if you could invest your efforts over there, as we’re – like I mentioned above – still missing field group cloning.

    P.p.s.: Surroundings If you stick with RW Meta Box: GitHub Team has been so nice and even sponsered us a free Account at TenderApp. So we have a support channels and a pretty kool KnowledgeBase too. 🙂