您是否应该同时指定wp_register_script/style和wp_enqueue_script/style的依存关系? [英] Should you specify dependencies in both wp_register_script/style and wp_enqueue_script/style?

查看:59
本文介绍了您是否应该同时指定wp_register_script/style和wp_enqueue_script/style的依存关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在注册脚本并指定依赖项后,在将其加入队列后,是否应该再次指定依赖项?

I was wondering if, after registering a scripts and specifying a dependency, upon enqueuing it, should you specify the dependency again?

按照这种好逻辑,我会说不,但是我没有真正的主意...您的灯很感激!

By that good'ol logic I would say no, but I have no real idea... Your lights are appreciated!

示例1

add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {

  if( ! is_admin() ) {

    wp_register_script( 'script_one_js', '//path/to/script/one.js' );
    wp_register_script( 'script_two_js', '//path/to/script/two.js', array( 'script_one_js' ) );

    // Specifying dependency again
    wp_enqueue_script( 'script_one_js' );
    wp_enqueue_script( 'script_two_js', array( 'script_one_js' ) );

  };

};

示例2

add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {

  if( ! is_admin() ) {

    wp_register_script( 'script_one_js', '//path/to/script/one.js' );
    wp_register_script( 'script_two_js', '//path/to/script/two.js', array( 'script_one_js' ) );

    // NOT specifying dependency again
    wp_enqueue_script( 'script_one_js' );
    wp_enqueue_script( 'script_two_js' );

  };

};

推荐答案

不,您不需要重复指定依赖项.

No, you don't need to specify the dependencies twice.

要了解为什么存在该选项,您需要查看 wp_enqueue_script wp_register_script 的用途.

To understand why the option is there, you need to look at what wp_enqueue_script and wp_register_script are for.

您无需完全注册脚本就可以入队,只需使用 wp_enqueue_script 即可,如果尚未注册,它将进行注册-这就是为什么您拥有这两个函数具有相同的选项(包括设置依赖项).

You don't have to register the script at all before you enqueue it, you can just use wp_enqueue_script on its own and it will register it if its not already registered - this is why you have the same options (including setting the dependencies) in both functions.

例如,您可以在functions.php中添加此代码,它将同时注册和排队脚本:

For example you can have this in your functions.php ad it will both register and enqueue the scripts at the same time:

add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
    if( ! is_admin() ) {
        wp_enqueue_script( 'script_1_js', '//path/to/script/one.js' );
        wp_enqueue_script( 'script_2_js', '//path/to/script/two.js', array( 'script_1_js' ) );
    }
}

那为什么要麻烦注册脚本呢?有很多优点,例如:

Why bother registering a script then? There are a number of advantages, such as:

  1. 您可以一次将具有正确依赖项和其他设置的脚本注册到一个位置,然后仅将它们加载到需要它们的页面或情况中.除了不必加载脚本之外,这还意味着您无需您必须在使用该脚本的 wp_enqueue_script 函数的每个位置填写所有参数.
  1. You can register a script once with the correct dependencies and other settings in one place, and then only load them in the pages or situations you need them in. As well as not loading scripts unnecessarily, this also means you don't have to fill in all the parameters every place you use the wp_enqueue_script function for that script.

例如,您可以在functions.php中使用它:

For example you can have this in your functions.php:

add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
    if( ! is_admin() ) {
        wp_register_script( 'script_1_js', '//path/to/script/one.js' );
        wp_register_script( 'script_2_js', '//path/to/script/two.js', array( 'script_1_js' ), '1.1', true );
    }
}

现在,例如,如果您仅需要在2个特定页面模板中加载 script_two_js ,则可以将 wp_enqueue_script('script_1_js'); 添加到这些模板中,而无需每次添加依赖性版本,因为它从注册时就已经知道了.

Now if you only need to load script_two_js in 2 particular page templates for example, you can add wp_enqueue_script( 'script_1_js' ); into to those templates without having to add the dependencies, version every time, because it already knows this from when it was registered.

  1. 您还可以使用 wp_register_script 注册脚本,如果您有另一个脚本具有依赖于此脚本的脚本,则它将在该脚本之前自动加载而无需排队./li>
  1. You can also register your script using wp_register_script, and if you have another script have have this script in its dependencies, then it will automatically get loaded before that script without the need to enqueue it.

例如,如果您有four.js,它依赖于其他3个脚本,例如

For example, if you have four.js which depends on 3 other scripts, e.g.

add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
    if( ! is_admin() ) {
        wp_register_script( 'script_1_js', '//path/to/script/one.js' );
        wp_register_script( 'script_2_js', '//path/to/script/two.js' );
        wp_register_script( 'script_3_js', '//path/to/script/three.js' );
        wp_register_script( 'script_4_js', '//path/to/script/four.js', array( 'script_1_js', 'script_2_js', 'script_3_js' );
    }
}

现在您可以使用 wp_enqueue_script('script_4_js'); 加载脚本,它将自动加载脚本一,二和三.

Now you can load the script with wp_enqueue_script( 'script_4_js' ); at it will automatically load scripts one, two and three.

希望这有助于回答您的问题!

Hope this helps answer your question!

这篇关于您是否应该同时指定wp_register_script/style和wp_enqueue_script/style的依存关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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