为什么要为wp_enqueue_scripts插入wp_head()? [英] Why should I insert wp_head() for wp_enqueue_scripts?

查看:115
本文介绍了为什么要为wp_enqueue_scripts插入wp_head()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将此代码添加到functions.php中,但startwordpress_scripts()函数未运行。

I have add this code to functions.php but startwordpress_scripts() function didn't run.

function startwordpress_scripts() {
    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '3.3.6' );
    wp_enqueue_style( 'blog', get_template_directory_uri() . '/css/blog.css' );
    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '3.3.6', true );
}

add_action( 'wp_enqueue_scripts', 'startwordpress_scripts' );

所以我在header.php中添加了以下代码,但效果很好。

So I added following code in header.php and they worked well.

<?php wp_head(); ?>

现在我想知道wp_head()函数的神奇作用。
谢谢。

Now I want to know the magic role of wp_head() function. Thanks.

推荐答案

在WordPress中,操作过滤器被视为挂钩。钩子允许我们修改WordPress默认行为而不修改核心中的代码。

In WordPress, actions and filters are considered as hooks. Hooks allow us to modify the WordPress default behaviour without modifying the code found in the core.

任何时候你有一个 add_action('xxx','回调'),当 do_action('xxx')时,将调用回调函数执行。

Anytime you have an add_action('xxx', 'callback'), the callback function will be called when do_action('xxx') is executed.

换句话说:当你有 add_action('wp_enqueue_scripts','startwordpress_scripts'); 时,这意味着当执行 do_action('wp_enqueue_scripts')时,WordPress将运行 startwordpress_scripts()

In other words: when you have add_action( 'wp_enqueue_scripts', 'startwordpress_scripts' );, it means that WordPress will run startwordpress_scripts() when do_action('wp_enqueue_scripts') is executed.

现在,让我们看一下WordPress核心中的代码。

Now, let's look at the code in the WordPress core.

如果你看一下它的函数定义, wp_head() do_action('wp_head')的快捷方式;

If you look at its function definition, wp_head() is a "shortcut" to do_action( 'wp_head' );

function wp_head() {
        /**
         * Print scripts or data in the head tag on the front end.
         *
         * @since 1.5.0
         */
        do_action( 'wp_head' );
}

换句话说, wp_head()将执行使用 add_action('wp_head)定义的所有回调')

In other words, wp_head() will execute all callbacks that were defined with add_action('wp_head').

如果您现在查看 wp-includes / default-filters.php 文件,你会看到:

If you now look at the wp-includes/default-filters.php file, you'll see:

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

这意味着当 wp_head()是在你的模板中遇到一个名为 wp_enqueue_scripts()的函数正在执行,因为它被挂钩到 wp_head ,如图所示上面的代码行。

It means that when wp_head() is encountered in your template, a function called wp_enqueue_scripts() is being executed, since it is hooked to wp_head, shown in the line of code above.

的函数定义wp_enqueue_scripts()是:

function wp_enqueue_scripts() {
    /**
     * Fires when scripts and styles are enqueued.
     *
     * @since 2.8.0
     */
    do_action( 'wp_enqueue_scripts' );
}

do_action('wp_enqueue_scripts'); 上面是告诉WordPress执行所定义的所有 add_action('wp_enqueue_scripts','callback')的回调函数。

The do_action( 'wp_enqueue_scripts' ); above is what tells WordPress to execute the callback function of all add_action('wp_enqueue_scripts', 'callback') that are defined.

简而言之:


  1. wp_head()调用 do_action('wp_head')

  2. do_action('wp_head')执行所有 add_action的回调函数('wp_head','callback')

  3. wp_enqueue_scripts()是一个回调 add_action('wp_head','callback')

  4. wp_enqueue_scripts ()调用 do_action('wp_enqueue_scripts')

  5. do_action('wp_enqueue_scripts ')执行所有 add_action的回调函数('wp_enqueue_scripts','回调')

  6. startwordpress_scripts() add_action('wp_enq'的回调ueue_scripts','callback')

  7. 包含在 startwordpress_scripts()中定义的JS / CSS

  1. wp_head() calls do_action('wp_head')
  2. do_action('wp_head') executes callback functions of all add_action('wp_head','callback')
  3. wp_enqueue_scripts() is one callback of add_action('wp_head','callback')
  4. wp_enqueue_scripts() calls do_action('wp_enqueue_scripts')
  5. do_action('wp_enqueue_scripts') executes callback functions of all add_action('wp_enqueue_scripts','callback')
  6. startwordpress_scripts() is one callback of add_action('wp_enqueue_scripts','callback')
  7. Your JS/CSS defined in startwordpress_scripts() are included

这篇关于为什么要为wp_enqueue_scripts插入wp_head()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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