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 am trying to figure out why my Ajax response is always 0. I am trying to load in meta data into a Jquery UI Dialog box based on the ID of the link clicked. All of this is in a single.php template file - not sure if that is the cause or not.

Here is my code(edited for space):

            jQuery( '#meta-dialog-box' ).dialog({
            width: "500px",
            autoOpen: false,
            show: "blind",
            hide: "explode",
            resizeable: false,
            draggable: false,
            modal: true,
            position: "center",
            closeText: "X",
            dialogClass: "co-meta-dialog-wrapper"
        });

        jQuery('a.co-meta-dialog').click(function(){
            var id = $(this).attr('id');
            var taxonomy = id.replace("co-","");
            taxonomy = taxonomy.replace("-dialog","");
            var title = $(this).text();
            jQuery( "#meta-dialog-box" ).dialog( "option", "title", title );
            jQuery( "#meta-dialog-box" ).dialog( "open" );
            jQuery( "#meta-dialog-box" ).html( '<img width="62px" height="62px" style="margin:0 auto;" src="<?php bloginfo('template_directory');?>/images/loading.gif" alt="loading..." />' );             

            //$( "#meta-dialog-box" ).html('Meta Dialog Box For: ' + id);

            switch( id ){
                case 'co-location-dialog':
                    dialog_action = 'show_co_location_dialog';
                break;

                case 'co-contact-dialog':
                    dialog_action = 'show_co_contact_dialog';
                break;

                //and so on....

                default:
                break;
            }//end switch

            jQuery.post(            
                UiAjax.ajaxurl,
                {
                    action : dialog_action,
                    postid: '<?php echo $postid; ?>',
                    tax: taxonomy
                },          
                function( response, data, status ) {
                    jQuery('#meta-dialog-box').html(response);
                    //alert(data + ' ' + status);
                }
            );
            return false;           
        });

I have check Firebug and my action is getting through correctly. So it seems my PHP call back is not being fired correctly. All my call back PHP functions have die(); at the end of them. And are set to print the meta data. I tried echo and return but still no luck.

I have all my ajax actions in my functions.php file like so:

//location|address
add_action('wp_ajax_show_co_location_dialog', array('DS_Lingerie','get_dialog_address') );
//contact info 
add_action('wp_ajax_show_co_contact_dialog', array('DS_Lingerie','get_dialog_contact_info') );

Do I need to set up a plugin to do this? Or am I missing something else entirely? I have been banging my head against the wall for about 6 hours now and cannot figure out what I am doing wrong here.

Any and all help is greatly appreciated! I may need to rethink how I have this set up, perhaps use a different modal/dialog function or something like that. Suggestions and insights are 100% welcome! Thanks in advance.

share|improve this question
    
Any progress on this question? Comment, further info? Anything? –  kaiser May 19 '13 at 17:03
add comment

1 Answer

You can't add AJAX calls from public templates.

  1. They have to get added much earlier
  2. The file that passes the call is admin-ajax.php for a reason: Ajax calls must not be hidden outside admin. For e.g. wrapping your call inside ! is_admin() will let it fail.
  3. Template files are not meant to hold actual function definition and such. This is where Plugins, Mu-Plugins or your themes functions.php file come into the game
share|improve this answer
add comment

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.