dreamworld2007 2014-10-28 05:42
浏览 69

我需要在JavaScript文件中使用PHP,因为Wordpress,我该怎么做或者是一个更好的选择

I am using a Google Map Plugin, This One to be exact.

Anyways that google map plugin gives me the options to change some settings like custom markers and a lot of other useful things. So I changed the main marker with my own image and it works great on my static HTML site but in Wordpress it does not and this is why.

So lets say I load the plugin in as so:

  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
  <script type="text/javascript" src="js/gmaps.js"></script>

now I have some options I can change in my footer or in WORDPRESS through my themes_js and load that in my functions.php and here they are:

var map = new GMaps({
    div: '#map',
    lat: 94.09454,
    lng: -219.35574
});

map.drawOverlay({
    lat: 94.09454,
    lng: -219.35574,
    content: '<div class="map-logo"><img src="images/logo.png" alt=""></div>'
});

Now the problem is in content when I try to change my image Wordpress will not get the image if it's just src="images/logo.png" it would have to be src="<?php bloginfo('template_directory'); ?>/images/logo.png" and the thing is the themes.js file I call all my javascript in wont read PHP and I need to use <?php bloginfo('template_directory'); ?>.

So how can I go about showing my image because the only way I can customize it is through the parameters they have provided through javascript and I can't just use HTML to call my custom marker image called logo.

Im sure im not the only one that has ran into a problem like so. Anyone have an idea??

UPDATE

So I have tried the wp_localize_script method and 2 things happen.

  1. I dont see my image not as in a broken link but cannot see it at all
  2. my entire URL displays in the header for some reason

Here is my functions.php where I called the wp_localize_script:

/**
 * Enqueue scripts and styles.
 */
function loading() {

    global $wp_scripts;

    wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/css/bootstrap.css' );

    wp_enqueue_style( 'main-style', get_stylesheet_uri() );

    wp_enqueue_style( 'owlcarousel_css', get_template_directory_uri() . '/owl-carousel/owl.carousel.css' );

    wp_enqueue_style( 'owltheme_css', get_template_directory_uri() . '/owl-carousel/owl.theme.css' );

    wp_enqueue_style( 'animate_css', get_template_directory_uri() . '/css/animate.css' );

    wp_enqueue_style( 'google_fonts', 'http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' );

    wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/js/bootstrap.js', array('jquery'), '', true );

    wp_enqueue_script( 'owl_js', get_template_directory_uri() . '/owl-carousel/owl.carousel.js', array('jquery'), '', true );

    wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery', 'bootstrap_js'), '', true );

    wp_enqueue_script( 'viewport_js', get_template_directory_uri() . '/js/jquery.viewportchecker.js', array('jquery'), '', true );



    wp_enqueue_script( 'maps_js', get_template_directory_uri() . '/js/gmaps.js', array('jquery'), '', true );

    wp_enqueue_script('maps_custom', get_stylesheet_directory_uri() . '/js/maps-custom.js');

    wp_localize_script('maps_custom', 'mapsCustom', array(

    'templateUrl' => bloginfo('template_directory'),));



    wp_register_script ('html5_shiv', 'https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js', '', '', false );

    wp_register_script ('respond_js', 'https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js', '', '', false );

    $wp_scripts->add_data( 'html5_shiv', 'conditional', 'lt IE 9' );

    $wp_scripts->add_data( 'respond_js', 'conditional', 'lt IE 9' );

}
add_action( 'wp_enqueue_scripts', 'loading' );

and here is the maps_js that I call my map on:

jQuery(document).ready(function( $ ) {

    var href = mapsCustom.templateUrl + '/images/logo.png';

    var map = new GMaps({
    div: '#map',
    lat: 22.22222,
    lng: -22.22222
});

map.drawOverlay({
    lat: 22.22222,
    lng: -22.22222,
    content: $href

      }); 
});
  • 写回答

1条回答 默认 最新

  • dongmin3754 2014-10-28 06:05
    关注

    Use wp_localize_script() to pass any kind of data to your loaded scripts, in this case we need bloginfo('template_directory'):

    wp_enqueue_script('my-script', get_stylesheet_directory_uri() . '/js/my-script.js');
    wp_localize_script('my-script', 'myScript', array(
    'templateUrl' => bloginfo('template_directory'),));
    

    Now you will have access to myScript.pluginsUrl in your script file:

    var href = myScript.templateUrl + '/path/to/resource';
    

    Generally, you can not use PHP in your JS file.So for that wordpress provide wp_localize_script().With the help of that you can localize your path and then pass that localize to the JS file.So basically it will work.

    Above I have given example how can you achieve.Modify it according to your need.

    Hope this will solve your problem.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮