Wordpress隐藏(移除)新增/修改文章页面中的meta boxes

来源:互联网 发布:深圳软件协会 编辑:程序博客网 时间:2024/05/18 09:30
对于一个多作者的`Wordpress`网站,我们有时候需要限制作者能够接触到的编辑选项,例如给文章加标签,或者禁用某些插件的选项。

这个时候我们需要用到do_meta_boxes或者admin_menu这两个hooks。注意,如果要移除插件生成的meta boxes,我们必须要使用do_meta_boxes而非admin_menu,因为后者触发得太早了。

好了,假设我们要给所有非adminsubadmin(自定义权限角色)用户禁用标签meta box,那么在functions.php里,我们需要添加以下代码:

/*** Function to remove the tag meta box from post-editing page for users other than admins and subadmins*/function remove_meta_boxes(){    if (!current_user_can('administrator') && !current_user_can('subadmin'))    {        remove_meta_box('tagsdiv-post_tag', 'post', 'side');    }}add_action('do_meta_boxes', 'remove_meta_boxes');

可以看到,我们使用了remove_meta_box这个自带函数来去除meta box。要注意的是第三个参数,指的是这个meta box的在屏幕上的显示位置,有normal, sideadvanced三种选项,这里我们的tag box位于最右边,所以我们用了side

函数具体的参数及使用方法,可以参见:
https://codex.wordpress.org/Function_Reference/remove_meta_box
其中常见的可以移除的meta boxesid如下:

'authordiv' – Author metabox'categorydiv' – Categories metabox.'commentstatusdiv' – Comments status metabox (discussion)'commentsdiv' – Comments metabox'formatdiv' – Formats metabox'pageparentdiv' – Attributes metabox'postcustom' – Custom fields metabox'postexcerpt' – Excerpt metabox'postimagediv' – Featured image metabox'revisionsdiv' – Revisions metabox'slugdiv' – Slug metabox'submitdiv' – Date, status, and update/save metabox'tagsdiv-post_tag' – Tags metabox'tagsdiv-{$tax-name}' - Custom taxonomies metabox'{$tax-name}div' - Hierarchical custom taxonomies metabox'trackbacksdiv' – Trackbacks metabox...

那如果我们需要修改某个meta box的标题呢?最好的办法就是先用remove_meta_box这个函数把原有的box去掉,然后再插入一个同样的box,只是将title替换成了我们需要的标题:

function remove_meta_boxes(){    if (!current_user_can('administrator') && !current_user_can('subadmin'))    {        remove_meta_box('tagsdiv-post_tag', 'post', 'side');        remove_meta_box('postimagediv', 'post', 'side');        add_meta_box('postimagediv', '缩略图片', 'post_thumbnail_meta_box', 'post', 'side', 'high');    }}add_action('do_meta_boxes', 'remove_meta_boxes');

具体的函数使用请参考:
https://developer.wordpress.org/reference/functions/add_meta_box/

阅读全文
0 0
原创粉丝点击