POST编辑屏幕:如何检查META_BOX是否已注册?

时间:2011-02-23 作者:kaiser

我有一些自定义的帖子类型,在那里我取消了“标题”元框的注册。现在我总是以“自动草稿”作为标题。为了避免这种情况,我想添加一个分类法的名称作为标题。但是我如何检查元框“title”是否存在?

2 个回复
最合适的回答,由SO网友:t31os 整理而成

是否直接检查metabox全局?

$wp_meta_boxes[\'YOURTYPE\'] 应保存给定类型的元数据库上的数据。

快速查看wp-admin/includes/template.php 但我看不到从数组中提取metabox信息的任何方便函数,因此您可能需要创建自己的代码来循环或从中提取必要的信息。

当然,请确保在add_meta_boxes 操作(否则它们可能在您运行代码时不存在)。

SO网友:Trinity Branding

我找到了这个有效的代码。

参考号:https://gist.github.com/trinitybranding/72066ca34584535dccbe3290dee02791

我重命名了原始函数名。

function __hasMetabox( $meta_box_id, $post_type = false ){
  global $wp_meta_boxes, $grist_meta_box_found;
    $grist_meta_box_found = false; // assume not found by default
    // if meta boxes are not yet set up, let\'s issue an error
    if( empty( $wp_meta_boxes ) or ! is_array( $wp_meta_boxes ) )
        return new WP_Error( \'missing-meta-boxes\', \'global $wp_meta_boxes is not the expected array\' );
    // should we only look at meta boxes for a specific post type?
    if ( $post_type )
        $meta_boxes = $wp_meta_boxes[ $post_type ];
    else
        $meta_boxes = $wp_meta_boxes;
    // step through each meta box registration and check if the supplied id exists
    array_walk_recursive( $meta_boxes, function( $value, $key, $meta ){
        global $grist_meta_box_found;
        if ( $key === \'id\' and strtolower( $value ) === strtolower( $meta )  )
            $grist_meta_box_found = true;
    }, $meta_box_id );
    $return = $grist_meta_box_found; // temp store the return value
    unset( $grist_meta_box_found );  // remove var from from global space
    return $return;
}
那就做点像。。。

add_action(\'add_meta_boxes\', function() {
  global $wp_meta_boxes, $post;
  if (__hasMetabox(\'submitdiv\', $post->post_type))
  $wp_meta_boxes[$post->post_type][\'side\'][\'core\'][\'submitdiv\'][\'title\'] = \'Amazing!!!\';
});
输出示例:

enter image description here

结束

相关推荐

在编辑帖子中使用AJAX自定义Metabox

我正在尝试创建一个带有多个文本字段的自定义元框。其想法是,最初有一个文本框,然后您可以单击按钮“添加新内容”,然后添加另一个文本框(使用Ajax;无需为要创建的文本框发布或更新帖子)。我正在使用以下代码(如下),但它根本不起作用。我完全被卡住了;真的需要一些帮助。如果有人能帮我解决这个问题,我甚至愿意捐赠一些现金。http://pastebin.com/usRhiJJ4