title: How to pass PHP values to js script in Wordpress using AJAX
body: <p>I have tried both wp_localize_script and wp_add_inline_script : it doesn't work the way I expect.</p>
  <p>I have this in my plugin's PHP file (it is enqueued with wp_enqueue_scripts somewhere else but I don't need to write the code here right ?) :</p>
  <pre><code>        wp_register_script(
              'custom-profile-script',
              &quot;{$this-&gt;plugin_url}js/custom-profile-plugin.js&quot;,
              array( 'jquery' ),
              null,
              false
          );
  
          wp_enqueue_script(
              'custom-profile-script'
          );
  
          wp_localize_script(
              'custom-profile-plugin',
              'wp_ajax',
              array( 
                  'ajax_url' =&gt; admin_url( 'admin-ajax.php' ), 
                  'ajaxnonce' =&gt; wp_create_nonce( 'ajax_post_validation' ),
                  'plugin_url' =&gt; &quot;url&quot;
              )
          );
          wp_add_inline_script( 'custom-profile-plugin', 'const PHPVAR = ' . json_encode( array(
                  'plugin_url' =&gt; &quot;url&quot;
          ) ), 'before' );
  </code></pre>
  <p>And then, in my js file :</p>
  <pre><code>var data = {
          action: 'custom_user_plugin_update_meta_rating_value', 
          security: wp_ajax.ajaxnonce
      };
  
  $.post(wp_ajax.ajax_url, data, function(result) {
  
          console.log(wp_ajax);
          console.log(PHPVAR.plugin_url);
  
  });
  </code></pre>
  <p>The first console log (related to wp_localize_script) is an object that contains &quot;ajax_url&quot; and &quot;ajax_nonce&quot; with the right values, but &quot;plugin_url&quot; doesn't appear and is UNDEFINED when I call it using wp_ajax.plugin_url.</p>
  <p>The second console log (related to wp_add_inline_script) tells me that PHPVAR is not defined...</p>
  <p>What I don't understand is why the first two values are passed correctly using localize script, but not the third one and, furthermore, why wp_add_inline_script doesn't work.</p>