Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wordpress将变量传递给javascript_Javascript_Jquery_Wordpress - Fatal编程技术网

wordpress将变量传递给javascript

wordpress将变量传递给javascript,javascript,jquery,wordpress,Javascript,Jquery,Wordpress,我试图从插件向javascript文件传递一个变量。以下是我的插件代码: <?php /** * @package JS Test * @version 1.0 */ /* Plugin Name: JS Test Description: JS Test. */ add_action('init', 'my_init'); add_action( 'wp', 'test_js' ); function my_init() { wp_enqueue_script( 'jqu

我试图从插件向javascript文件传递一个变量。以下是我的插件代码:

<?php
/**
 * @package JS Test
 * @version 1.0
 */
/*
Plugin Name: JS Test
Description: JS Test.
*/


add_action('init', 'my_init');
add_action( 'wp', 'test_js' );

function my_init() {
  wp_enqueue_script( 'jquery' );
}

function test_js() {

   wp_register_script ('testjs', plugins_url('jstest.js', __FILE__));
   wp_enqueue_script ('testjs');
   $my_arr = array('my array',
     'name' => 'Test',
   );
   $my_json_str = json_encode($my_arr); 

   $params = array(
     'my_arr' => $my_json_str,
   );

   wp_enqueue_script('my-java-script');
   wp_localize_script('my-java-script', 'php_params', $params);
}
我得到的是JavaScript中的以下错误: ReferenceError:未定义php_参数
有人知道我做错了什么吗?

您不需要为本地化参数将脚本排队,您可以使用testjs来实现这一点,下面是我为测试而制作的一个快速插件:

<?php
/*
Plugin Name: PHP to JS
Plugin URI: http://en.bainternet.info
Description: wordpress passing variable to javascript 
http://stackoverflow.com/questions/12761904/wordpress-passing-variable-to-javascript
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/

add_action('init', 'my_init');
add_action( 'wp', 'test_js' );

function my_init() {
 /wp_enqueue_script( 'jquery' );
}

function test_js() {

   wp_register_script ('testjs', plugins_url('jstest.js', __FILE__));
   wp_enqueue_script ('testjs');
   $my_arr = array('my array',
     'name' => 'Test',
   );
   $my_json_str = json_encode($my_arr); 

   $params = array(
     'my_arr' => $my_json_str,
   );


   wp_localize_script('testjs', 'php_params', $params);
}

add_action('wp_footer','footertestjs');

function footertestjs(){
    ?>
    <script>
      jQuery(document).ready(function() {
        alert ('test');
        var my_json_str = php_params.my_arr.replace(/&quot;/g, '"');
        var my_php_arr = jQuery.parseJSON(my_json_str);
        alert(php_params);    
      });
    </script>
    <?php
}

jQuery(文档).ready(函数(){
警报(“测试”);
var my_json_str=php_params.my_arr.replace(/“/g,”);
var my_php_arr=jQuery.parseJSON(my_json_str);
警报(php_参数);
});

wp_localize_脚本是在
jstest.js
之后调用的,因此路径中没有任何内容,因此错误您必须在js文件的某个地方定义php_parms var。你只是神奇地期待它现在就在那里。@MarcB,他实际上是通过wp_本地化_脚本传递它的。
<?php
/*
Plugin Name: PHP to JS
Plugin URI: http://en.bainternet.info
Description: wordpress passing variable to javascript 
http://stackoverflow.com/questions/12761904/wordpress-passing-variable-to-javascript
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/

add_action('init', 'my_init');
add_action( 'wp', 'test_js' );

function my_init() {
 /wp_enqueue_script( 'jquery' );
}

function test_js() {

   wp_register_script ('testjs', plugins_url('jstest.js', __FILE__));
   wp_enqueue_script ('testjs');
   $my_arr = array('my array',
     'name' => 'Test',
   );
   $my_json_str = json_encode($my_arr); 

   $params = array(
     'my_arr' => $my_json_str,
   );


   wp_localize_script('testjs', 'php_params', $params);
}

add_action('wp_footer','footertestjs');

function footertestjs(){
    ?>
    <script>
      jQuery(document).ready(function() {
        alert ('test');
        var my_json_str = php_params.my_arr.replace(/&quot;/g, '"');
        var my_php_arr = jQuery.parseJSON(my_json_str);
        alert(php_params);    
      });
    </script>
    <?php
}