wordpress 后台取消显示custom fields(remove_meta_box)

在WordPress开发中,为了简化用户界面,通常需要隐藏custom fields(自定义字段)框。通过`remove_meta_box`函数可以实现这一目标,将参数`id`设置为`'postcustom'`。然而,该函数必须在特定的action中调用才能生效,不能直接在`register_post_type`参数中处理。官方文档提供了正确的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我们用wordpress做网站的时候,存储自定义数据,需要用到custom fields,也就是自定义字段。不过,我们在提供给用户使用后台的时候,不能向他们展示custom fields框,一是让用户理解这要怎么用会比较麻烦,二是没有一个友好的界面,使用起来并不方便。
所以,我们在默认情况下,是隐藏掉custom fields框的。
怎么取消掉呢,后台的custom fields框,也是一种meta box,在remove_meta_box函数里有介绍:

      remove_meta_box( $id, $page, $context );

$id
(string) (required) Value of the id attribute of the HTML element to remove. Some of the available id values are given below:
‘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

‘{$tax-name}div’ - Hierarchical custom taxonomies metabox
‘trackbacksdiv’ – Trackbacks metabox

所以,custom fields用remove_meta_box就可以隐藏,第一个参数换成‘postcustom’即可。
不过,函数使用的到时候,不能简单的在function里面添加这么一句,官方文档有相关实例:

function remove_page_excerpt_field() {
    remove_meta_box( 'postexcerpt' , 'page' , 'normal' ); 
}
add_action( 'admin_menu' , 'remove_page_excerpt_field' );

要在action里使用才可以生效,
而且不要使用register_post_type里面的参数来取消custom fields的显示。

remove_meta_box

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值