据我所知,管理脚本应该通过admin_enqueue_scripts
钩子和所有其他脚本wp_enqueue_scripts
因此,我设置了以下函数,以清晰、有序的方式注册和排队所有脚本。
我的问题是,如果我需要某些脚本(例如。jquery validate 插件)在管理和前端?在这种情况下,注册和排队脚本的建议方法是什么?使用不同的$句柄注册两次,或通过wp_enqueue_scripts
只有这样,才有可能在需要时不被调用?(我的意思是,为什么其他人会admin_enqueue_scripts
如果不提前提供这些脚本,是否存在?
我非常感谢有人向我解释这一点,以充分理解wp中排队脚本的细微差别。谢谢
我的代码:
// REGISTER ALL NON-ADMIN SCRIPTS
add_action( \'wp_enqueue_scripts\', \'register_all_non_admin_scripts\' );
function register_all_non_admin_scripts() {
wp_register_script( ... );
wp_register_script( ... );
}
// ENQUEUE NON-ADMIN SCRIPTS CONDITIONALLY
add_action( \'wp_enqueue_scripts\', \'enqueue_scripts_where_required\' );
function enqueue_scripts_where_required() {
// scripts to be loaded at all times
wp_enqueue_script( \'\' );
// scripts to be loaded conditionaly
if( is_page( \'\' ) ) {
wp_enqueue_style( \'\' );
}
}
// REGISTER ALL ADMIN SCRIPTS
add_action( \'admin_enqueue_scripts\', \'register_all_admin_scripts\' );
function register_all_admin_scripts(){
wp_register_script( ... );
wp_register_script( ... );
}
// ENQUEUE ADMIN SCRIPTS
add_action( \'admin_enqueue_scripts\', \'enqueue_admin_contact_cpt_js\' );
function enqueue_admin_contact_cpt_js(){
global $post_type;
// scripts to be loaded at all times
wp_enqueue_script( \'\' );
// scripts to be loaded conditionaly by post type
if( \'contact\' == $post_type ){
wp_enqueue_script( \'\' );
...
}
}