WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

        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 );
share|improve this answer

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

Your Answer


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.