Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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
Javascript wp_localize_script()仅在本地主机上工作_Javascript_Php_Jquery_Wordpress - Fatal编程技术网

Javascript wp_localize_script()仅在本地主机上工作

Javascript wp_localize_script()仅在本地主机上工作,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我试图在我的wordpress插件中使用传单JS。下面是我注册脚本的代码 add_action( 'wp_enqueue_scripts', 'ea_map_load_scripts' ); function ea_map_load_scripts(){ wp_register_script( 'ea_map_leaflet_js', plugins_url('js/leaflet.js', __FILE__), array('jquery'),false,true); wp_r

我试图在我的wordpress插件中使用传单JS。下面是我注册脚本的代码

add_action( 'wp_enqueue_scripts', 'ea_map_load_scripts' );
function ea_map_load_scripts(){
    wp_register_script( 'ea_map_leaflet_js', plugins_url('js/leaflet.js', __FILE__), array('jquery'),false,true);
    wp_register_script( 'ea_map_js', plugins_url('js/map.js', __FILE__), array('jquery'),false,true);
    //                                                                                          ^^
    //I need to use this parameter otherwhise the leafletjs map is not charged------------------|
    wp_enqueue_script('jquery');
    wp_enqueue_script( 'ea_map_leaflet_js' );
    wp_enqueue_script( 'ea_map_js' );
    ...
}
在我的主题中,我通过do_action()调用该函数,尝试将一些php变量传递给我的javascript插件:

add_action('wp_ea_map','wp_ea_map');
function wp_ea_map() {
    $ea_params = Array(
        'id' => get_the_ID(),
        'latitudine' => 'xx.xxx',
        'longitudine' => '-xx.xxx',
    );

    wp_localize_script( 'ea_map_js', 'marker', $map_params );
}
问题是,在我的本地主机上,同一个数据库中的同一代码运行良好,但没有设置live server标记变量。下面是我使用传递变量的js代码:

jQuery(document).ready(function($){
    var map = L.map('map').setView([marker.latitudine, marker.longitudine], 11);
    ....
});

是的,我确信我在两台服务器中都有相同的代码,并且没有缓存。

我认为这是一个时间问题。加载'ea_map_js'时,尚未调用动作'wp_ea_map'

我也找到了一个在实时服务器上工作的解决方案,将wp_localize_脚本函数移动到ea_map_load_scripts()中wp_enqueue_脚本('ea_map_js')之后。代码现在看起来像:

add_action( 'wp_enqueue_scripts', 'ea_map_load_scripts' );
function ea_map_load_scripts(){
    wp_register_script( 'ea_map_leaflet_js', plugins_url('js/leaflet.js', __FILE__), array('jquery'),false,true);
    wp_register_script( 'ea_map_js', plugins_url('js/map.js', __FILE__), array('jquery'),false,true);

    wp_enqueue_script('jquery');
    wp_enqueue_script( 'ea_map_leaflet_js' );
    wp_enqueue_script( 'ea_map_js' );

    $ea_params = Array(
        'id' => get_the_ID(),
        'latitudine' => 'xx.xxx',
       'longitudine' => '-xx.xxx',
    );

    wp_localize_script( 'ea_map_js', 'marker', $map_params );
    ...
}
我尝试了另一种解决方案,使用另一个钩子通过回音将变量粘贴到代码中:

add_action('wp_head','wp_baro');
function wp_baro(){
   $post_id = get_the_ID();

   echo '<script>
        /* <![CDATA[ */
            var marker = {  
                "id" : "'.$post_id.'",
                "latitudine" : "'.xx.xxxx.'",
                "longitudine" : "'.yy.yyyy.'"};
        /* ]]> */
        </script>';
}
add_action('wp_head','wp_baro');
函数wp_baro(){
$post_id=获取_id();
回声'
/*  */
';
}