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'm using: wp_localize_script( $handle, $namespace, $variables ); to declare some variables before the initial AJAX call is made, but I imagine I can't do the same thing again within a callback function? I need to declare a newly created ID for use in another function, what's the best way?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

The right way is to post the data back to the WordPress admin ajax url then in your PHP function access them using the $_POST variables.

Example jQuery function that passed a category id back to wordpress:

function cat_edit_get() {
    jQuery( "#loading-animation").show();
    var catID = jQuery("#cat :selected").val();
    var text = jQuery("#cat :selected").text();
    jQuery('#replace').html(text);

    jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: {"action": "cat-editor-get", cat: catID },
        success: function(response) {
            jQuery("#the-list").html(response);
            jQuery("#loading-animation").hide();
            return false;

        }
    });

}

Then you can access the variables in WordPress by hooking into the wp_ajax action. Here I'm only passing category id chosen from a drop down select:

add_action ( 'wp_ajax_cat-editor-get', 'cat_editor_get' );
    function cat_editor_get () {
        $cat_id = $_POST[ 'cat' ];
        $preview_cats = get_post_meta ( 82799, '_' . $cat_id . 'saved', true );
        $item_order = $preview_cats[ 'cat_order' ];
        $post_ids = explode ( ",", $item_order, 30 );
        global $post;
        if ( count ( $post_ids ) < 5 ) {
            $args = array (
                'category_id' => $cat_id,
                'length' => 30,

            );

            $post_ids = wndsq_get_post_ids ( $args );
        }
share|improve this answer
add comment

What you could do is break down your functions and let the first function call return the newly created ID as a response. Then you could take the response and use wp_localize_script() to pass the ID to the other function call located script file and then do the function call. Haven't tried this, but I think this should work.

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.