【问题标题】:WordPress wp_localize_script : PHP variable not recognized by JS scriptWordPress wp_localize_script:JS脚本无法识别PHP变量
【发布时间】:2018-12-23 18:46:47
【问题描述】:

我正在尝试通过我的子主题在我的 WordPress 中修改脚本 (ajaxcalls.js)。我在子主题 (ajaxcalls_child.js) 中创建了该文件的副本。

我在 function.php 文件中添加了以下代码,以便将旧 JS 文件出列,将新文件入队并传递变量 ajaxcalls_vars 其中 ajaxcalls_child .js 使用:

<?php

//made sure here that the action execute after all the other scripts are loaded
add_action('wp_enqueue_scripts', 'wpestate_load_child_scripts', 100);

function wpestate_load_child_scripts() {
    wp_dequeue_script('wpestate_ajaxcalls');

    wp_enqueue_script('ajaxcalls_child', get_stylesheet_directory_uri().'/js/ajaxcalls_child.js', array('jquery'),'1.0', true);

    wp_localize_script(‘ajaxcalls_child’, ‘ajaxcalls_vars’, array(
        'contact_name'     =>  esc_html__( 'Your Name','wprentals'),
        'contact_email'    =>  esc_html__( 'Your Email','wprentals'),
        'contact_phone'    =>  esc_html__( 'Your Phone','wprentals'),
        'contact_comment'  =>  esc_html__( 'Your Message','wprentals'),
        'adv_contact_name' =>  esc_html__( 'Your Name','wprentals'),
        'adv_email'        =>  esc_html__( 'Your Email','wprentals'),
        'adv_phone'        =>  esc_html__( 'Your Phone','wprentals'),
        'adv_comment'      =>  esc_html__( 'Your Message','wprentals'),
        'adv_search'       =>  esc_html__( 'Send Message','wprentals'),
        'admin_url'        =>  get_admin_url(),
        'login_redirect'   =>  $login_redirect,
        'login_loading'    =>  esc_html__( 'Sending user info, please wait...','wprentals'),
        'userid'           =>  $userID,
        'prop_featured'    =>  esc_html__( 'Property is featured','wprentals'),
        'no_prop_featured' =>  esc_html__( 'You have used all the "Featured" listings in your package.','wprentals'),
        'favorite'         =>  esc_html__( 'Favorite','wprentals').'<i class="fas fa-heart"></i>',
        'add_favorite'     =>  esc_html__( 'Add to Favorites','wprentals'),
        'remove_favorite'  =>  esc_html__( 'remove from favorites','wprentals'),
        'add_favorite_unit'=>  esc_html__( 'add to favorites','wprentals'),
        'saving'           =>  esc_html__( 'saving..','wprentals'),
        'sending'          =>  esc_html__( 'sending message..','wprentals'),
        'reserve'          =>  esc_html__( 'Reserve Period','wprentals'),
        'paypal'           =>  esc_html__( 'Connecting to Paypal! Please wait...','wprentals'),
        'stripecancel'     =>  esc_html__( 'subscription will be cancelled at the end of the current period','wprentals'),
        'max_month_no'     =>  intval   ( get_option('wp_estate_month_no_show','') ),
        'processing'       =>  esc_html__( 'processing..','wprentals'),
        'home'             =>  get_site_url(),
        'delete_account'   =>  esc_html__('Confirm your ACCOUNT DELETION request! Clicking the button below will result your account data will be deleted. This means you will no longer be able to login to your account and access your account information: My Profile, My Reservations, My bookings, Invoices. This operation CAN NOT BE REVERSED!','wprentals'),
        'adv_search_what_half' =>  $adv_search_what_half,
        'adv_search_how_half'  =>  $adv_search_how_half,
        'adv_search_type'      =>  $adv_search_type
    ));
}

现在访问我的网站后,我可以看到旧脚本已被新脚本替换,但我可以在 chrome 控制台上看到,只要 a 函数在新脚本中需要它,ajaxcalls_vars 就没有定义。

Chrome 控制台出错

所以新脚本在那里,但我的变量 ajaxcalls_vars 没有根据 ajaxcalls_child.js 定义,因此,我认为我的方式有问题尝试将该变量传递给新脚本。

在查看了类似问题后,我理解了其他类型的错误,并尝试将该代码改进为我上面分享的代码。但仍然无法正确处理。

【问题讨论】:

    标签: javascript php wordpress wordpress-theming


    【解决方案1】:

    wp_localize_script(‘ajaxcalls_child’, ‘ajaxcalls_vars’, array(... 中前 2 个参数周围的单引号不是真正的单引号。尝试删除它们并替换为' 字符wp_localize_script('ajaxcalls_child', 'ajaxcalls_vars', array(... 一旦我这样做了,该对象就可以在入队脚本中使用(在 xampp wp 5.0 上测试)

    【讨论】:

    • 确实现在可以使用了……时不时地感觉自己像个初学者是这个社区的魅力所在。非常感谢!
    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多