3

I am stuck in strange situation I am trying to pass PHP variable in JS by WordPress wp_localize_script and try to show it in console.log but it is outputting null. here is my code in functions.php

$conv = 1.36;
echo $conv;

add_action('wp_enqueue_scripts','cassets');
function cassets(){
  wp_enqueue_script("all-script",get_template_directory_uri().'/all-script.js',array('jquery'),'',true);
  $rate= array(
    'conv' => $conv,
  );
  wp_localize_script( 'all-script', 'rate', $rate);

}

and in all-script.js

var conv = rate.conv;
console.log(conv);

In console window it shows null but php echo value shows right.

Thanks in advance.

6
  • Try wrapping your javascript in $(document).ready(function () { // your script here }) Commented Mar 12, 2019 at 17:27
  • @tom-m I tried but didn't work. Commented Mar 12, 2019 at 17:30
  • @Teemu - This isn't a duplicate of that question. WordPress allows you to easily pass PHP variables to JavaScript with the wp_localize_script() function. His variables are just used out of scope. Commented Mar 12, 2019 at 17:33
  • @Xhynk Yeah, I realized it almost immediately after closing, reopened now. Commented Mar 12, 2019 at 17:34
  • $conv is not defined in your function. Commented Mar 12, 2019 at 17:36

2 Answers 2

5

You're using your $conv variable out of scope. Take a look at PHP's Variable Scope documentation. You're defining $conv in the global scope, but referencing a local scope $conv in your cassets() function.

You need to use the function scoped $conv, either by defining it inside, or passing it to the function as a global variable or pass it as a Reference.

Here's a few examples:

Defining within the scope:

add_action('wp_enqueue_scripts','cassets');
function cassets(){
    $conv = 1.36;

    wp_enqueue_script( 'all-script', get_template_directory_uri().'/all-script.js', array('jquery'), '', true );

    $rate = array(
        'conv' => $conv,
    );

    wp_localize_script( 'all-script', 'rate', $rate );
}

Passing it to the function as a global variable:

$conv = 1.36;

add_action('wp_enqueue_scripts', 'cassets' );
function cassets(){
    global $conv;

    wp_enqueue_script( 'all-script', get_template_directory_uri().'/all-script.js', array('jquery'), '', true );

    $rate = array(
        'conv' => $conv,
    );

    wp_localize_script( 'all-script', 'rate', $rate );
}

Passing it via closure:

$conv = 1.36;

add_action('wp_enqueue_scripts', function() use($conv){
    wp_enqueue_script( 'all-script', get_template_directory_uri().'/all-script.js', array('jquery'), '', true );

    $rate = array(
        'conv' => $conv,
    );

    wp_localize_script( 'all-script', 'rate', $rate );
});
Sign up to request clarification or add additional context in comments.

Comments

2

Your problem is, that you defined $conv outside your function.

Inside your function $conv was is undefined. (I think u should get a warning from php too).

Try this:

add_action('wp_enqueue_scripts','cassets');
function cassets(){
  $conv = 1.36;
  wp_enqueue_script("all-script",get_template_directory_uri().'/all-script.js',array('jquery'),'',true);
  $rate= array(
    'conv' => $conv,
  );
  wp_localize_script( 'all-script', 'rate', $rate);

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.