wp_localize_script无法使用jquery句柄 [英] wp_localize_script not working with jquery handle

查看:89
本文介绍了wp_localize_script无法使用jquery句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试欺骗jquery句柄,因为我在WP项目中使用grunt / bower来创建我所有插件和脚本的连接缩小版本。我想在其中包含jQuery 2.1.3,并将带有wordpress的旧版jquery出列,但是为了确保其他插件工作,我想(可能有点像hacky方式)使用jquery处理我的脚本。

I'm trying to spoof the jquery handle as I use grunt / bower in my WP projects to create concatenated minified builds of all my plugins and scripts. I'd like to include jQuery 2.1.3 within this, and dequeue the older version of jquery that ships with wordpress, but in order to make sure other plugins work I thought to (maybe in a bit of a hacky way) use the jquery handle for my script.

不幸的是,似乎本地化脚本不会解雇。我正在做的是:

Unfortunately it seems like localize script wont fire though. What I'm doing is this :

    wp_deregister_script('jquery'); 

    wp_register_script( 'jquery', get_template_directory_uri().'/public/js/all.min.js', array(), false, true );

    wp_enqueue_script( 'jquery' );

    $localize_data = array(
    ....all the stuff I need in my scripts....
    );
    wp_localize_script( 'jquery', 'nona', $localize_data );

这显然包含在一个调用适当钩子的函数中,它适用于自定义句柄,但是有兼容性问题,除了创建一个数组并手动将其转换为javascript对象之外的任何想法?

This is obviously wrapped in a function thats called on the appropriate hook, it works with a custom handle, but then there are compatibility issues, any ideas other than just creating an a array and converting it to a javascript object manually?

任何帮助都将不胜感激。

any help would be appreciated.

推荐答案

函数 wp_localize_script 调用 wp_scripts() - > localize ,位于的顶部该功能有:

if ( $handle === 'jquery' )
    $handle = 'jquery-core';

它将句柄更改为 jquery-core ,所以去看看默认脚本已注册我们发现 jquery 不直接引用javascript文件,而是通过依赖项 jquery-core jquery-migrate

It changes the handle to jquery-core, so going to see where the default scripts are registered we find that jquery doesn't reference the javascript file directly but via the dependencies jquery-core and jquery-migrate:

// jQuery
$scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.11.2' );
$scripts->add( 'jquery-core', '/wp-includes/js/jquery/jquery.js', array(), '1.11.2' );
$scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '1.2.1' );

我会尝试将句柄更改为 jquery-core ,类似这样:

I would try to change the handle to jquery-core, something like this:

add_action('wp_enqueue_scripts', 'jquery_localized', 1);

function jquery_localized() {
    wp_deregister_script('jquery');
    wp_deregister_script('jquery-core');

    wp_enqueue_script( 'jquery-core', get_template_directory_uri().'/public/js/all.min.js', array(), false, true );

    $localize_data = array(
    // ....all the stuff I need in my scripts....
    );
    wp_localize_script( 'jquery-core', 'nona', $localize_data );

    // this for compatibility purpose
    wp_register_script( 'jquery', false, array( 'jquery-core' ), false, true );
}

请注意,我添加了 1 优先在同一个钩子上的其他人之前执行此功能。

Note that I added 1 as priority to execute this function before others on the same hook.

这篇关于wp_localize_script无法使用jquery句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆