正確加載 jQuery、Javascript 和 CSS 到你的WordPress網(wǎng)站也許是一件比較痛苦的事情。 本文將講解如何使用WordPress官方推薦的方式來加載腳本/ CSS。
有兩種常用的 add_action 鉤子可以加載 腳本和CSS到WordPress:
- init: 確保始終為您的網(wǎng)站頭部加載腳本和CSS(如果使用home.php,index.php或一個(gè)模板文件),以及其他“前端”文章、頁面和模板樣式。
- wp_enqueue_scripts:“適當(dāng)”的鉤子方法,并不總是有效的,根據(jù)你的WordPress設(shè)置。
下面的所有例子都在WordPress多站點(diǎn)模式、WordPress 3.4.2 通過測(cè)試(如果不支持后續(xù)版本,請(qǐng)留言告知)
加載外部 jQuery 庫和主題自定義的腳本、樣式
下面這個(gè)例子在 add_action 鉤子中使用 init。使用 init 有兩個(gè)原因,一是因?yàn)槲覀冋谧NWordPress默認(rèn)的jQuery庫,然后加載谷歌的jQuery庫;二是確保在WordPress的頭部就加載腳本和CSS。
使用if ( !is_admin() )是為了確保這些腳本和css只在前端加載,不會(huì)再后臺(tái)管理界面加載。
/** Google jQuery Library, Custom jQuery and CSS Files */
function myScripts() {
wp_register_script( 'google', 'http://ajax.googleapis.com/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默認(rèn) jQuery 庫和主題自定義的腳本、樣式
第3行:使用 array(‘jquery’) 是為了告訴 WordPress 這個(gè) jquery.js 是依賴WordPress 的jQuery庫文件,從而使 jquery.js 在WordPress jQuery庫文件后加載。
/** 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’是媒體屏幕調(diào)用,確保 print.css 在網(wǎng)站的打印機(jī)中的文件加載時(shí)才加載。
/** 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
如果你要在文章或頁面加載唯一的腳本,那就應(yīng)該使用 wp_enqueue_scripts 替換 init。使用 wp_enqueue_scripts 僅僅只會(huì)在前臺(tái)加載腳本和CSS,不會(huì)在后臺(tái)管理界面加載,所以沒必要使用 !is_admin() 判斷。
使用 is_single() 只在文章加載腳本或CSS
第 3 行的 # 替換為文章的ID就可以讓腳本和css只加載到那篇文章。當(dāng)然,如果直接使用 is_single() (不填I(lǐng)D),就會(huì)在所有文章加載腳本和CSS。
/** 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只加載到那個(gè)頁面。當(dāng)然,如果直接使用 is_single() (不填I(lǐng)D),就會(huì)在所有頁面加載腳本和CSS。
/** 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 加載腳本到后臺(tái)
這個(gè)例子將在整個(gè)后臺(tái)管理界面加載腳本和CSS。這個(gè)方法不推薦用在插件上,除非插件重建了整個(gè)后臺(tái)管理區(qū)。
第 10 行使用 admin_enqueue_scripts 替換了 init 或 wp_enqueue_scripts
第 5、6 行,如果你要自定義后臺(tái)管理區(qū),你可以需要禁用默認(rèn)的WordPress CSS調(diào)用。
/** 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 行:我無法弄清楚如何在在登錄頁面注冊(cè)/排序 CSS文件,所以這行手動(dòng)添加樣式表。
第 10-14行:用來移除WordPress默認(rèn)的樣式表。
/** 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 ,而插件應(yīng)該用 plugins_url ,因?yàn)槲募菑牟寮夸涍M(jìn)行加載的。
從插件加載腳本和CSS
這個(gè)例子將在整個(gè)網(wǎng)站前端加載腳本和CSS。
/** 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到后臺(tái)管理區(qū)
如果你需要在整個(gè)后臺(tái)管理區(qū)加載腳本和CSS,就使用 admin_enqueue_scripts 替換 init。
/** 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到插件設(shè)置頁面
例子只會(huì)加載所需的腳本和CSS到插件設(shè)置頁面,不會(huì)在管理區(qū)的其他頁面加載。
第 3 行:自定義 page= 后面的值為你的插件設(shè)置頁面
/** 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 庫移動(dòng)到頁腳
你不能將WordPress默認(rèn)的jQuery 庫移動(dòng)到頁面底部,但是你可以將自定義的jQuery 或其他外部jQuery 庫(比如Google的)移動(dòng)到底部。不要將CSS移動(dòng)到頁面底部。
第 3、4 行:最后的 ‘true’告訴WordPress在頁面底部加載這些腳本。
/** Moves jQuery to Footer */
function footerScript() {
wp_register_script('jquery', ("http://ajax.googleapis.com/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' );
根據(jù)不用的用戶角色和功能加載jQuery和CSS
如果你的網(wǎng)站有作者、編輯和其他管理員,你可能需要通過 jQuery 來為他們顯示不同的信息。你需要使用 current_user_can 確定登錄的用戶的 角色和功能 。
下面三個(gè)例子中,如果用戶已經(jīng)登錄,將在整個(gè)網(wǎng)站加載這些腳本和CSS。使用 !is_admin() 包裝 enqueue_script 確保只在前臺(tái)加載,或者在 add_action 使用 admin_enqueue_scripts 就可以確保只在后臺(tái)管理區(qū)加載。
為可以“編輯文章”的管理員加載腳本和CSS
只對(duì)超級(jí)管理員和網(wǎng)站管理員生效
/** 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,就可以被合并成一個(gè)單一的函數(shù)。 換句話說,您可以使用多個(gè) if 語句在一個(gè)函數(shù)中分裂了你的腳本和CSS調(diào)用,如:if_admin!if_admin,is_page,is_single和current_user_can的,因?yàn)槊看问褂孟嗤腶dd_action的init。
- 原文:http://technerdia.com/1789_include-jquery-css.html
- 編譯:倡萌@WordPress大學(xué)





新手小白提問:樓主說的這些代碼需要放到具體哪個(gè)文件的那個(gè)行代碼后面啊?
每個(gè)主題都有一個(gè) functions.php 文件,可以添加到這個(gè)文件里面,具體介紹請(qǐng)看 http://www.ydqwiac.cn/wordpress-functions-php.html
感謝您的回復(fù),不過我遇到的問題是:現(xiàn)在我遇到的問題就是最近突然網(wǎng)站前端包括Wordpress后端登錄界面以及登錄后都無法加載所有的CSS樣式文件JS文件。F12報(bào)錯(cuò)是找不到所有的CSS,JS文件。所有的CSS文件,JS文件都放在服務(wù)器上了。望請(qǐng)您有空時(shí)不吝賜教,萬分感謝。
貼出部分F12報(bào)錯(cuò)信息如下:
wordpress-admin-menu.min.css:1 Failed to load resource: the server responded with a status of 404 ()
thickbox.css:1 Failed to load resource: the server responded with a status of 404 ()
admin.min.css:1 Failed to load resource: the server responded with a status of 404 ()
jqvmap.min.css:1 Failed to load resource: the server responded with a status of 404 ()
admin-global-1740.css:1 Failed to load resource: the server responded with a status of 404 ()
dashboard-1740.css:1 Failed to load resource: the server responded with a status of 404 ()
wpseo-dismissible-1740.css:1 Failed to load resource: the server responded with a status of 404 ()
admin.css:1 Failed to load resource: the server responded with a status of 404 ()
admin-top-bar.min.css:1 Failed to load resource: the server responded with a status of 404 ()
elementor-icons.min.css:1 Failed to load resource: the server responded with a status of 404 ()
common.min.css:1 Failed to load resource: the server responded with a status of 404 ()
admin.min.css:1 Failed to load resource: the server responded with a status of 404 ()
mediaelementplayer-legacy.min.css:1 Failed to load resource: the server responded with a status of 404 ()
wp-mediaelement.min.css:1 Failed to load resource: the server responded with a status of 404 ()
imgareaselect.css:1 Failed to load resource: the server responded with a status of 404 ()
menu.css:1 Failed to load resource: the server responded with a status of 404 ()
dashboard.css:1 Failed to load resource: the server responded with a status of 404 ()
adminbar-1740.css:1 Failed to load resource: the server responded with a status of 404 ()
jquery.min.js:1 Failed to load resource: the server responded with a status of 404 ()
jquery-migrate.min.js:1 Failed to load resource: the server responded with a status of 404 ()
utils.min.js:1 Failed to load resource: the server responded with a status of 404 ()
regenerator-runtime.min.js:1 Failed to load resource: the server responded with a status of 404 ()
wp-polyfill.min.js:1 Failed to load resource: the server responded with a status of 404 ()
core.min.js:1 Failed to load resource: the server responded with a status of 404 ()
chart.min.js:1 Failed to load resource: the server responded with a status of 404 ()
jquery.vmap.min.js:1 Failed to load resource: the server responded with a status of 404 ()
jquery.vmap.world.min.js:1 Failed to load resource: the server responded with a status of 404 ()
admin.min.js:1 Failed to load resource: the server responded with a status of 404 ()
admin-global-1740.js:1 Failed to load resource: the server responded with a status of 404 ()
admin.js:1 Failed to load resource: the server responded with a status of 404 ()
moxie.min.js:1 Failed to load resource: the server responded with a status of 404 ()
plupload.min.js:1 Failed to load resource: the server responded with a status of 404 ()
datepicker.min.js:1 Failed to load resource: the server responded with a status of 404 ()
index.php:85 Uncaught ReferenceError: jQuery is not defined