Javascript 和 CSS 如何加载到 WordPress主题前端、后端或插件

2023-05-2309:23:56WEB前端开发Comments1,052 views字数 6707阅读模式

正确加载 jQuery、Javascript 和 CSS 到你的WordPress网站也许是一件比较痛苦的事情。 本文将讲解如何使用WordPress官方推荐的方式来加载脚本/ CSS。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

有两种常用的 add_action 钩子可以加载 脚本和CSS到WordPress:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

  • init: 确保始终为您的网站头部加载脚本和CSS(如果使用home.php,index.php或一个模板文件),以及其他“前端”文章、页面和模板样式。
  • wp_enqueue_scripts:“适当”的钩子方法,并不总是有效的,根据你的WordPress设置。

下面的所有例子都在WordPress多站点模式、WordPress 3.4.2 通过测试(如果不支持后续版本,请留言告知)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

加载外部 jQuery 库和主题自定义的脚本、样式

下面这个例子在 add_action 钩子中使用 init。使用 init 有两个原因,一是因为我们正在注销WordPress默认的jQuery库,然后加载谷歌的jQuery库;二是确保在WordPress的头部就加载脚本和CSS。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

使用if ( !is_admin() )是为了确保这些脚本和css只在前端加载,不会再后台管理界面加载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Google jQuery Library, Custom jQuery and CSS Files */  
function myScripts() {  
        wp_register_script( 'google', 'http://ajax.lug.ustc.edu.cn/ajax/libs/jquery/1.8.2/jquery.js' );  
        wp_register_script( 'default', get_template_directory_uri() . '/jquery.js' );  
        wp_register_style( 'default', get_template_directory_uri() . '/style.css' );  
    if ( !is_admin() ) { /** Load Scripts and Style on Website Only */  
        wp_deregister_script( 'jquery' );  
        wp_enqueue_script( 'google' );  
        wp_enqueue_script( 'default' );  
        wp_enqueue_style( 'default' );  
    }  
}  
add_action( 'init', 'myScripts' );

加载WP默认 jQuery 库和主题自定义的脚本、样式

第3行:使用 array('jquery') 是为了告诉 WordPress 这个 jquery.js 是依赖WordPress 的jQuery库文件,从而使 jquery.js 在WordPress jQuery库文件后加载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Add Custom jQuery and CSS files to a Theme */  
function myScripts() {  
        wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', array('jquery'), '' );  
        wp_register_style( 'default', get_template_directory_uri() . '/style.css' );  
    if ( !is_admin() ) { /** Load Scripts and Style on Website Only */  
        wp_enqueue_script( 'default' );  
        wp_enqueue_style( 'default' );  
    }  
}  
add_action( 'init', 'myScripts' );

加载 print.css 到你的WordPress主题

第 3 行:最后的 ‘print’是媒体屏幕调用,确保 print.css 在网站的打印机中的文件加载时才加载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Adding a Print Stylesheet to a Theme */  
function myPrintCss() {  
        wp_register_style( 'print', get_template_directory_uri() . '/print.css', '', '', 'print' );  
    if ( !is_admin() ) { /** Load Scripts and Style on Website Only */  
        wp_enqueue_style( 'print' );  
    }  
}  
add_action( 'init', 'myPrintCss' );

使用 wp_enqueue_scripts 替换 init

如果你要在文章或页面加载唯一的脚本,那就应该使用 wp_enqueue_scripts 替换 init。使用 wp_enqueue_scripts 仅仅只会在前台加载脚本和CSS,不会在后台管理界面加载,所以没必要使用 !is_admin() 判断。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

使用 is_single() 只在文章加载脚本或CSS

第 3 行的 # 替换为文章的ID就可以让脚本和css只加载到那篇文章。当然,如果直接使用 is_single() (不填ID),就会在所有文章加载脚本和CSS。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Adding Scripts To A Unique Post */  
function myScripts() {  
    if ( is_single(#) ) { /** Load Scripts and Style on Posts Only */  
        /** Add jQuery and/or CSS Enqueue */  
    }  
}  
add_action( 'wp_enqueue_scripts', 'myScripts' );

使用 is_page() 只在页面加载脚本或CSS

第 3 行的 # 替换为页面的ID就可以让脚本和css只加载到那个页面。当然,如果直接使用 is_single() (不填ID),就会在所有页面加载脚本和CSS。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Adding Scripts To A Unique Page */  
function myScripts() {  
    if ( is_page(#) ) { /** Load Scripts and Style on Pages Only */  
        /** Add jQuery and/or CSS Enqueue */  
    }  
}  
add_action( 'wp_enqueue_scripts', 'myScripts' );

使用 admin_enqueue_scripts 加载脚本到后台

这个例子将在整个后台管理界面加载脚本和CSS。这个方法不推荐用在插件上,除非插件重建了整个后台管理区。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

第 10 行使用 admin_enqueue_scripts 替换了 init 或 wp_enqueue_scripts文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

第 5、6 行,如果你要自定义后台管理区,你可以需要禁用默认的WordPress CSS调用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Adding Scripts To The WordPress Admin Area Only */  
function myAdminScripts() {  
    wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', array('jquery'), '' );  
    wp_enqueue_script( 'default' );  
    //wp_deregister_style( 'ie' ); /** removes ie stylesheet */  
    //wp_deregister_style( 'colors' ); /** disables default css */  
    wp_register_style( 'default', get_template_directory_uri() . '/style.css',  array(), '', 'all' );  
    wp_enqueue_style( 'default' );  
}  
add_action( 'admin_enqueue_scripts', 'myAdminScripts' );

加载脚本和CSS到WordPress登录界面

第 6 行:我无法弄清楚如何在在登录页面注册/排序 CSS文件,所以这行手动添加样式表。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

第 10-14行:用来移除WordPress默认的样式表。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Adding Scripts To The WordPress Login Page */  
function myLoginScripts() {  
    wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', array('jquery'), '' );  
    wp_enqueue_script( 'default' );  
?>  
    <link rel='stylesheet' id='default-css' href='<?php echo get_template_directory_uri() . '/style.css';?>' type='text/css' media='all' />  
<?php }  
add_action( 'login_enqueue_scripts', 'myLoginScripts' );  
/** Deregister the login css files */  
function removeScripts() {  
    wp_deregister_style( 'wp-admin' );  
    wp_deregister_style( 'colors-fresh' );  
}  
add_action( 'login_init', 'removeScripts' );

加载脚本和CSS到WordPress插件

WordPress插件加载脚本和CSS也是常见的。主要的不同之处在于文件的 URL。主题使用的是 get_template_directory_uri ,而插件应该用 plugins_url ,因为文件是从插件目录进行加载的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

从插件加载脚本和CSS文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

这个例子将在整个网站前端加载脚本和CSS。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Global Plugin Scripts for Outside of Website */  
function pluginScripts() {  
    wp_register_script( 'plugin', plugins_url( 'jquery.js' , __FILE__ ), array('jquery'), '' );  
    wp_register_style( 'plugin', plugins_url( 'style.css' , __FILE__ ) );  
    if ( !is_admin() ) { /** Load Scripts and Style on Website Only */  
        wp_enqueue_script( 'plugin' );  
        wp_enqueue_style( 'plugin' );  
    }  
}  
add_action( 'init', 'pluginScripts' );

从插件加载脚本和CSS到后台管理区

如果你需要在整个后台管理区加载脚本和CSS,就使用 admin_enqueue_scripts 替换 init。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Global Plugin Scripts for The WordPress Admin Area */  
function pluginScripts() {  
    wp_register_script( 'plugin', plugins_url( 'jquery1.js' , __FILE__ ), array('jquery'), '' );  
    wp_enqueue_script( 'plugin' );  
    wp_register_style( 'plugin', plugins_url( 'style1.css' , __FILE__ ) );  
    wp_enqueue_style( 'plugin' );  
}  
add_action( 'admin_enqueue_scripts', 'pluginScripts' );

从插件加载脚本和CSS到插件设置页面

例子只会加载所需的脚本和CSS到插件设置页面,不会在管理区的其他页面加载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

第 3 行:自定义 page= 后面的值为你的插件设置页面文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Adding Scripts On A Plugins Settings Page */  
function pluginScripts() {  
    if ( $_GET['page'] == "plugin_page_name.php" ) {  
        wp_register_script( 'plugin', plugins_url( 'jquery.js' , __FILE__ ), array('jquery'), '' );  
        wp_enqueue_script( 'plugin' );  
        wp_register_style( 'plugin', plugins_url( 'style.css' , __FILE__ ) );  
        wp_enqueue_style( 'plugin' );  
    }  
}  
add_action( 'admin_enqueue_scripts', 'pluginScripts' );

将 jQuery 库移动到页脚

你不能将WordPress默认的jQuery 库移动到页面底部,但是你可以将自定义的jQuery 或其他外部jQuery 库(比如Google的)移动到底部。不要将CSS移动到页面底部。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

第 3、4 行:最后的 ‘true’告诉WordPress在页面底部加载这些脚本。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Moves jQuery to Footer */  
function footerScript() {  
        wp_register_script('jquery', ("http://ajax.lug.ustc.edu.cn/ajax/libs/jquery/1.8.2/jquery.js"), false, '', true );  
        wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', false, '', true );  
    if ( !is_admin() ) { /** Load Scripts and Style on Website Only */  
        wp_deregister_script( 'jquery' );  
        wp_enqueue_script( 'jquery' );  
        wp_enqueue_script( 'default' );  
    }  
}  
add_action( 'init', 'footerScript' );

根据不用的用户角色和功能加载jQuery和CSS

如果你的网站有作者、编辑和其他管理员,你可能需要通过 jQuery 来为他们显示不同的信息。你需要使用 current_user_can 确定登录的用户的 角色和功能 。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

下面三个例子中,如果用户已经登录,将在整个网站加载这些脚本和CSS。使用 !is_admin() 包装 enqueue_script 确保只在前台加载,或者在 add_action 使用 admin_enqueue_scripts 就可以确保只在后台管理区加载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

为可以“编辑文章”的管理员加载脚本和CSS

只对超级管理员和网站管理员生效文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

/** Add CSS & jQuery based on Roles and Capabilities */  
function myScripts() {  
    if ( current_user_can('edit_posts') ) {  
        /** Add jQuery and/or CSS Enqueue */  
    }  
}  
add_action( 'init', 'myScripts' );

为所有登录用户加载脚本和CSS

/** Admins / Authors / Contributors /  Subscribers */  
function myScripts() {  
    if ( current_user_can('read') ) {  
        /** Add jQuery and/or CSS Enqueue */  
    }  
}  
add_action( 'init', 'myScripts' );

为管理员以外的已登录用户加载脚本和CSS

/** Disable for Super Admins / Admins enable for Authors / Contributors /  Subscribers */  
function myScripts() {  
    if ( current_user_can('read') && !current_user_can('edit_users') ) {  
        /** Add jQuery and/or CSS Enqueue */  
    }  
}  
add_action( 'init', 'myScripts' );

最后的提示

上面的例子如果使用相同的add_action,就可以被合并成一个单一的函数。 换句话说,您可以使用多个 if 语句在一个函数中分裂了你的脚本和CSS调用,如:if_admin!if_admin,is_page,is_single和current_user_can的,因为每次使用相同的add_action的init。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/41614.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/41614.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定