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?

2 Answers 2


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();

        type: 'POST',
        url: ajaxurl,
        data: {"action": "cat-editor-get", cat: catID },
        success: function(response) {
            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 );

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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