如果用户不是管理员,请注销jQuery? [英] If user is not admin, deregister jQuery?

查看:68
本文介绍了如果用户不是管理员,请注销jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望这不是重复的问题.我正在开始Wordpress主题开发的旅程.我一直在使用Codex作为文档来编写我创建的每个组件.但是,我遇到了一个摘要,由于某些原因,我无法理解它.

I really hope this is not a duplicate question. I'm beginning my journey into Wordpress theme development. I've been using the Codex as documentation to write each component I create. However, I have came across a snippet which for some reason I cannot get my head across it.

if ( !is_admin() ) {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', ( 'ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' ), false, null, true );
    wp_enqueue_script( 'jquery' );
}

阅读纯英语的代码段时说,如果用户不是admin,请注销jQuery,再次注册它,并在正确的时间发送页面时将其排队等待输出.当我可以简单地将jQuery添加到一组'< script></script>'中时,为什么要这样做呢?标签并完成它?

Reading the snippet in pure English it says, if user is not admin, deregister jQuery, register it again, and queue it for output when the page is sent at the right time. Why would I want to do this when I can simply add jQuery to a set of '<script></script>' tags and be done with it?

编辑

应该提到我在哪里发现了此片段.我在 http://digwp.com上的Blank Theme上的functions.php文件中找到它/2010/02/blank-wordpress-theme/

Should've mention where I discovered this snippet. I found it in the functions.php file on Blank Theme by http://digwp.com/2010/02/blank-wordpress-theme/

推荐答案

is_admin()检查如果当前显示的页面是管理员仪表板页面.要检查用户是否为管理员,请使用 current_user_can('administrator' )-甚至更好的是检查特定功能.

is_admin() checks if the current page being displayed is an admin Dashboard page. To check if the user is an admin, you'd use current_user_can('administrator') -- or, even better, check for a particular capability.

此代码的目的是保留WordPress随附的jQuery版本完整用于管理页面(该页面使用大量jQuery和AJAX),但在主题页面上加载另一个版本.他们可能试图加载比当时WordPress随附的版本更高的版本,或者他们可能只是试图从Google的CDN中加载该版本.

The purpose of this code is to leave the version of jQuery that comes with WordPress intact for admin pages (which use a lot of jQuery & AJAX) but load a different one on themed pages. They might have been trying to load a newer version than what shipped with WordPress at the time, or they could have just been trying to get it loaded off Google's CDN.

这篇关于如果用户不是管理员,请注销jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆