Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 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. :)

share|improve this answer
    
Awesome resource, thanks for sharing. –  Jared Jan 23 '12 at 5:35
    
I'm familiar with the RW Meta Box library, its awesome. I was developing my own classes purely as a learning exercise. I only use on for any jQuery events that need to be added after the dom loads. I'm actually invoking the admin ajax calls via a click event. Cloning Fieldsets is pretty simple, I have it working, but just not using admin ajax calls, which would be cleaner. I can post my implementation here once I've cleaned up the class. Thanks again for your comments. –  Dale Sattler Jan 23 '12 at 8:15
    
@noponies I really don't understand for what you bring admin ajax into the game. It makes sense if you want to save data, but unless you aren't interacting with the DB, plain ol'n'stupid JavaScript or jQuery is enough. Sidenote: Every ajax call that points to admin-ajax.php, actually reloads the whole WP Core. –  kaiser Jan 23 '12 at 14:56
    
Wow, I didn't know that admin-ajax.php forced a core reload, thats insane. I agree, it probably is overkill. For me it was a case of, 'well the functionality is there, so why not try to use it'. –  Dale Sattler Jan 23 '12 at 19:17
    
admin-ajax.php loads wp-load.php at the top. So, please either delete the Q as off topic or mark this A as solution. Thanks. –  kaiser Jan 23 '12 at 19:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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