为什么在我使用完整的wp_enQueue_style()时wp_Register_style()很重要?

时间:2013-11-25 作者:Mayeenul Islam

目前我正在研究一个主题,并在队列中加入了一些样式和脚本。当脚本排队时,我使用wp_register_script() 首先,然后使用wp_enqueue_script() - 因为我知道这是让脚本排队的正确方式。但在排队时,我只使用了wp_enqueue_style() 及其所有参数。但刚才我an answer s\\u ha\\u dum说wp_register_style() 将使主题儿童主题友好-我想知道,如果没有wp_register_style() 我只使用wp_enqueue_style() 我的主题与排队风格配合得很好。我把他们排成这样:

add_action( \'wp_enqueue_scripts\', \'wpse20131025_styles\' );

function wpse20131025_styles(){

    wp_enqueue_style( \'site-styles\', get_template_directory_uri() . \'/style.css\', \'\', \'\', \'screen\' );
    wp_enqueue_style( \'menu-styles\', get_template_directory_uri() . \'/css/menu.css\', \'\', \'\', \'screen and (min-device-width: 800px)\' );
    wp_enqueue_style( \'mobile-menu-styles\', get_template_directory_uri() . \'/css/mobile-menu.css\', \'\', \'\', \'screen and (max-device-width: 799px)\' );

}
wp_enqueue_style() 具有中存在的所有参数wp_register_style() ($handle、$src、$deps、$ver、$media)那我为什么要重复它们呢?或者,到底目的是什么wp_register_style() 除了wp_enqueue_style()?

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

首先,让我们假设关于这些函数,对样式有效的东西对脚本完全有效,但在回答中有一些例外情况与之相反。

之间的主要区别wp_enqueue_* 和各自的wp_register_* 功能是,第一个向队列中添加脚本/样式,第二个准备要添加的脚本/样式。

你可能已经知道了,但还有第二个区别;wp_register_* 可以用在每个钩子上,即使是早期的钩子init, 但是wp_enqueue_* 应在上使用wp_enqueue_scripts 挂钩(或admin_enqueue_scripts 对于后端)1。

使用这两个函数的典型场景是,当您想在主题init上注册脚本/样式,然后在某些页面上有条件地将它们排队,例如。

add_action(\'init\', \'my_register_styles\');

function my_register_styles() {
    wp_register_style( \'style1\', get_template_directory_uri() . \'/style1.css\' );
    wp_register_style( \'style2\', get_template_directory_uri() . \'/style2.css\' );
    wp_register_style( \'style3\', get_template_directory_uri() . \'/style3.css\' );
}

add_action( \'wp_enqueue_scripts\', \'my_enqueue_styles\' );

function my_enqueue_styles() {
    if ( is_front_page() ) {
        wp_enqueue_style( \'style3\' );
    } elseif ( is_page_template( \'special.php\' ) ) {
        wp_enqueue_style( \'style1\' );
        wp_enqueue_style( \'style2\' );
    } else {
        wp_enqueue_style( \'style1\' );
    }
}
通过这种方式,条件排队更具可读性,更不冗长。

此外,如果要将一个或多个已注册的样式/脚本也排入后端队列,可以在函数挂钩中使用已注册的句柄admin_enqueue_scripts 无需再次传递所有参数。

当然这对脚本更有用,因为wp_localize_script 在前面的场景中,可以在脚本注册后调用一次,然后在条件排队时,只需将脚本排队,即使它被多次使用:这样可以使代码更加简单和枯燥。

当您注册脚本/样式并将其排入队列后,这只是一项不必要的任务,实际上可以完全避免:

wp_register_style( \'style1\', get_template_directory_uri() . \'/style1.css\' );
wp_enqueue_style( \'style1\' );
像这样将样式(或脚本)排队没有好处,只需使用wp_enqueue_style 所有参数为wp_enqueue_style 你就完了。

关于儿童主题友谊,前一句也是正确的。在父主题使用时覆盖子主题中的样式wp_register_style 紧接着wp_enqueue_style, 您必须取消注册样式,然后使用另一个URL再次注册。如果家长只使用wp_enqueue_style, 在子主题中,可以使用wp_dequeue_style 并将新样式排队,因此在子主题中,两种情况下都需要两行代码。

然而,非常适合于子主题的是将排队和/或注册样式和脚本的函数包装到if ( ! function_exists( ... 通过这种方式,子主题可以在一个位置覆盖父主题样式和脚本:

if ( ! function_exists( \'my_register_styles\' ) ) {
    function my_register_styles() {
        wp_register_style( \'style1\', get_template_directory_uri() . \'/style1.css\' );
        wp_register_style( \'style2\', get_template_directory_uri() . \'/style2.css\' );
        wp_register_style( \'style3\', get_template_directory_uri() . \'/style3.css\' );
    }
}

if ( ! function_exists( \'my_enqueue_styles \') ) {
    function my_enqueue_styles() {
        if ( is_front_page() ) {
            wp_enqueue_style( \'style3\' );
        } elseif ( is_page_template( \'special.php\' ) ) {
            wp_enqueue_style( \'style1\' );
            wp_enqueue_style( \'style2\' );
        } else {
            wp_enqueue_style( \'style1\' );
        }
    }
}
现在在儿童主题中,可以编写另一个my_register_styles 和/或my_enqueue_styles 功能并更改所有样式(当然,如果需要的话),而无需逐个取消注册/退出样式。

以下是脚本和样式的另一个区别:wp_enqueue_script 可以在页面的正文中使用(通常在短代码中使用),脚本将放在页脚中。

结束

相关推荐