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 am trying to use wp_localize_script() to send some php values to a script.

The following are the part of the file inc/show-event.php.

if( $price ) {
    $pricefortwo    = ceil( ( 2 * $price) - ( 0.2 * $price ) );
    $savefortwo     = ( 2 * $price) - $pricefortwo;
    $priceforthree  = ceil( ( 3 * $price) - ( 0.333 * $price ) );
    $saveforthree   = ( 3 * $price) - $priceforthree;
    $priceforretake = ceil( 0.5 * $price );
    $saveforretake  = $price - $priceforretake;

    // Setting the session variables
    $_SESSION['price'] = $price;
    $_SESSION['price_for_two'] = $pricefortwo;
    $_SESSION['price_for_three'] = $priceforthree;
    $_SESSION['price_for_retake'] = $priceforretake;

    $session_array = array(
                'price'         => $_SESSION['price'],
                'price_for_two'     => $_SESSION['price_for_two'],
                'price_for_three'   => $_SESSION['price_for_three'],
                'price_for_retake'  => $_SESSION['price_for_retake']
    wp_localize_script( 'init_show_calendar', 'session_param', $session_array );

After this when I try and use the object name session_param in init_show_calendar.js file it throws a js error saying session_param is not defined. but when i use the following code in functions.php.

$session_array = array(
                'price'         => $_SESSION['price'],
                'price_for_two'     => $_SESSION['price_for_two'],
                'price_for_three'   => $_SESSION['price_for_three'],
                'price_for_retake'  => $_SESSION['price_for_retake']
wp_localize_script( 'init_show_calendar', 'session_param', $session_array );

it returns me the variable but it doesn't return the latest values, it returns the value stored in a page refresh.

FYI: The show-event.php is called in a plugin in the following way

add_action('wp_ajax_get_event', array($this, 'render_frontend_modal'));
function render_frontend_modal() {
    require_once AEC_PATH . 'inc/show-event.php';

Please let me know if you want any more information.

share|improve this question
I'm not sure I follow what you're doing. You can't localize a script in an AJAX callback, it has to be localized in the request that outputs the script, after the script is enqueued but before it is output to the page. – Milo Dec 3 '13 at 17:34
@Milo I want to use the session variables in my js script, so tried pass it to the script using wp_localize, can i do it any other way? – Maruti Mohanty Dec 3 '13 at 18:11
json_encode your $session_array and return that from your ajax handler. – Milo Dec 3 '13 at 21:56
up vote 1 down vote accepted

My need was to set different prices in the session and fetch those in my js script. I am using a jquery session plugin to set and get session, but it wasn't getting session set by php, so I was trying to send variables using wp_localize and set it in the js script.

Taking the clue from @Milo, I passed the values using json_encode and set them to session at the ajax handler end.

This is what i did:--

show-event.php file

 $output = array(
        'price'         => $_SESSION['price'],
        'price_for_two'     => $_SESSION['price_for_two'],
        'price_for_three'   => $_SESSION['price_for_three'],
        'price_for_retake'  => $_SESSION['price_for_retake']
    $this->json_encode( $output );

and in the ajax handler i did:--

// Setting the session value for the prices.
jQuery.session.set('price', data.price);
jQuery.session.set('price_for_two', data.price_for_two);
jQuery.session.set('price_for_three', data.price_for_three);
jQuery.session.set('price_for_retake', data.price_for_retake);

The above sets the session values for the different prices.

I could have also passed the array but I did this which is fine.

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.