外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>正确加载 Javascript 和 CSS 到 WordPress

正确加载 Javascript 和 CSS 到 WordPress

正确加载 jQueryJavascriptCSS 到你的WordPress网站也许是一件比较痛苦的事情。 本文将讲解如何使用WordPress官方推荐的方式来加载脚本/ CSS

有两种常用的 add_action 钩子可以加载 脚本和CSS到WordPress:

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

下面的所有例子都在WordPress多站点模式、WordPress 3.4.2 通过测试(如果不支持后续版本,请留言告知)

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

下面这个例子在 add_action 钩子中使用 init。使用 init 有两个原因,一是因为我们正在注销WordPress默认的jQuery库,然后加载谷歌的jQuery库;二是确保在WordPress的头部就加载脚本和CSS。

使用if ( !is_admin() )是为了确保这些脚本和css只在前端加载,不会再后台管理界面加载。

/** 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默认 jQuery 库和主题自定义的脚本、样式

第3行:使用 array(‘jquery’) 是为了告诉 WordPress 这个 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’是媒体屏幕调用,确保 print.css 在网站的打印机中的文件加载时才加载。

/** 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() 判断。

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

第 3 行的 # 替换为文章的ID就可以让脚本和css只加载到那篇文章。当然,如果直接使用 is_single() (不填ID),就会在所有文章加载脚本和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只加载到那个页面。当然,如果直接使用 is_single() (不填ID),就会在所有页面加载脚本和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 加载脚本到后台

这个例子将在整个后台管理界面加载脚本和CSS。这个方法不推荐用在插件上,除非插件重建了整个后台管理区。

第 10 行使用 admin_enqueue_scripts 替换了 init 或 wp_enqueue_scripts

第 5、6 行,如果你要自定义后台管理区,你可以需要禁用默认的WordPress CSS调用。

/** 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文件,所以这行手动添加样式表。

第 10-14行:用来移除WordPress默认的样式表。

/** 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 ,因为文件是从插件目录进行加载的。

从插件加载脚本和CSS

这个例子将在整个网站前端加载脚本和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到后台管理区

如果你需要在整个后台管理区加载脚本和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到插件设置页面

例子只会加载所需的脚本和CSS到插件设置页面,不会在管理区的其他页面加载。

第 3 行:自定义 page= 后面的值为你的插件设置页面

/** 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移动到页面底部。

第 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' );  

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

如果你的网站有作者、编辑和其他管理员,你可能需要通过 jQuery 来为他们显示不同的信息。你需要使用 current_user_can 确定登录的用户的 角色和功能

下面三个例子中,如果用户已经登录,将在整个网站加载这些脚本和CSS。使用 !is_admin() 包装 enqueue_script 确保只在前台加载,或者在 add_action 使用 admin_enqueue_scripts 就可以确保只在后台管理区加载。

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

只对超级管理员和网站管理员生效

/** 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。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

如何获取 WordPress 各类页面的链接

2013-5-17 10:09:23

WordPress开发

WordPress函数:wp_localize_script(脚本本地化)

2013-5-20 8:37:31

23 条回复 A文章作者 M管理员
  1. 新手小白提问:楼主说的这些代码需要放到具体哪个文件的那个行代码后面啊?

    • 倡萌

      感谢您的回复,不过我遇到的问题是:现在我遇到的问题就是最近突然网站前端包括Wordpress后端登录界面以及登录后都无法加载所有的CSS样式文件JS文件。F12报错是找不到所有的CSS,JS文件。所有的CSS文件,JS文件都放在服务器上了。望请您有空时不吝赐教,万分感谢。
      贴出部分F12报错信息如下:
      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

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索