wp_localize_script带手柄的jQuery - 字preSS脚本 [英] wp_localize_script with handle jquery - wordpress script

查看:142
本文介绍了wp_localize_script带手柄的jQuery - 字preSS脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决方案。我想更快的网页我Concat的所有的JS到一个文件中,并放置在页脚。我的一个JS是jQuery和我用一个钩子和功能:

I'm looking for next solution. I want faster website so I concat all JS to one file and placed in footer. One of my js is jQuery and I use next hook and function:

if (!function_exists("ef_theme_scripts")) {

    function ef_theme_scripts() {
        wp_deregister_script('jquery'); 
        wp_register_script('jquery', get_template_directory_uri() . '/js/min/script.min.js', null, null, true);
        wp_enqueue_script('jquery');
        $params = array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'ajax_nonce' => wp_create_nonce('user_nonce'),
        );
        wp_localize_script( 'jquery', 'ajax_object', $params );         
    }

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

我叫我把手jQuery的,因为可能有一些脚本(例如,从插件),它要使用jQuery,所以我需要添加一个称为该句柄jQuery的。

I called my handle "jquery" because there can be some scripts (e.g. from plugins) which wants use jquery so I need add called this handle "jquery".

一切都只是本地化的伟大工程。当我重新命名的脚本处理,例如,以定制的jQuery,然后wp_localize_script工作没有问题。

Everything works great except localize. When I rename script handle for example to "custom-jquery" then wp_localize_script works without problem.

我用WP 4.0.1。感谢您的帮助

I use WP 4.0.1. Thanks for help

推荐答案

我问过类似的问题,得到了在这里工作的解决方案:

I asked a similar question and got the working solution here :

<一个href="http://stackoverflow.com/questions/29723872/wp-localize-script-not-working-with-jquery-handle/29724589#29724589">wp_localize_script不工作与jQuery手柄

基本上是这样,是因为有那个WP工作原理与jQuery的处理方式,你需要

basically this has to do with the way that WP works with the jQuery handle, you'll need to

  1. DEREGISTER的jQuery
  2. DEREGISTER jquey核
  3. 使用jQuery的核心把手脚本
  4. 在结束寄存器的jQuery,与jQuery的核心一个扶养,并通过虚假的$ SCR属性

  1. deregister jquery
  2. deregister jquey-core
  3. Use the jquery-core handle on your script
  4. at the end register jquery, with a dependancy on jquery-core and passing false for the $scr attribute

wp_register_script('jQuery的',虚假,阵列('jQuery的核心),假的,真正的);

这篇关于wp_localize_script带手柄的jQuery - 字preSS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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