【问题标题】:How to add custom fields to a WordPress Plugin如何将自定义字段添加到 WordPress 插件
【发布时间】:2016-07-19 21:52:21
【问题描述】:

我的客户要求我添加一个自定义字段,他们将能够在 url 中输入该字段。帖子本身是一个自定义插件自定义帖子类型,这是我这部分的代码:

register_post_type( 'storylist',
    array(
        'labels' => $labels,
        'public' => false,
        'exclude_from_search' => true,
        'publicly_queryable' => false,
        'show_ui' => true,
        'supports' => array('title'),
    )
);
    add_filter( 'rwmb_meta_boxes', 'c_register_meta_boxes' );

}

function c_register_meta_boxes( $boxes ){
    $prefix = 'c_rwmb_';
    $boxes[] = array(
    'id'    => 'view',
    'title' => __('View Link', 'c_rwmb' ),
    'post_types'  => array('storylist'),
    'context'   => 'normal',
    'priority'  => 'high', 
    'fields' => array(
        array(
            'name'  => __('View URL', 'c_rwmb' ),
            'id'    => $prefix . 'view_url',
            'type' => 'text',
            'size'  => 60,
            'clone' =>  false
        ),
    )

);

   return $meta_boxes;
}

现在的问题是,当我转到帖子时,我什至看不到自定义元字段,是我遗漏了什么吗?

【问题讨论】:

  • 愚蠢的问题,但为了确定——你已经安装了Meta Box plugin,对吧?我很确定 rwmb_meta_boxes 是针对它的。
  • 查看代码,您应该返回$boxes,而不是$meta_boxes
  • @Hobo 你说得对,我很傻。谢谢。

标签: php wordpress custom-post-type


【解决方案1】:

自定义帖子类型(“storylist”)来自插件对吗?然后您不需要再次注册自定义帖子。您只需要为此帖子类型添加元字段并在更新帖子时保存其值。一旦我有使用自定义字段启用/禁用侧边栏的经验。我分享了我的代码。希望这会对你有所帮助。

<?php
add_action('admin_init','add_metabox_post_sidebar');
add_action('save_post','save_metabox_post_sidebar');
/*
 * Funtion to add a meta box to enable/disable the posts.
 */
function add_metabox_post_sidebar()
{
    add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high");
}

function enable_sidebar_posts(){
    global $post;
    $check=get_post_custom($post->ID );
    $checked_value = isset( $check['post_sidebar'] ) ? esc_attr( $check['post_sidebar'][0] ) : 'no';
    ?>

    <label for="post_sidebar">Enable Sidebar:</label>
    <input type="checkbox" name="post_sidebar" id="post_sidebar" <?php if($checked_value=="yes"){echo "checked=checked"; } ?> >
    <p><em>( Check to enable sidebar. )</em></p>
    <?php
}

/*
 * Save the Enable/Disable sidebar meta box value
 */
function save_metabox_post_sidebar($post_id)
{
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    $checked_value = isset( $_POST['post_sidebar'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, 'post_sidebar', $checked_value );


}

?>

在这里,我为帖子类型“post”添加了一个名为“post_sidebar”的自定义字段,您可以更改自己的字段,并将此行 add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high"); 中的帖子类型从“post”更改为“storylist”。

【讨论】:

  • 谢谢@Palanivelrajan。我认为这会有所帮助,我只需将其修改为文本字段而不是复选框。
【解决方案2】:

仅适用于需要通过plugin 添加MetaBox 字段的人,因为问题是:

如何将自定义字段添加到 WordPress 插件

function gffgfg_add_boxes( $meta_boxes ) {
    $prefix = 'some prefix';

    $meta_boxes[] = array(
        //metabox array
    );

    return $meta_boxes;
}

add_filter( 'rwmb_meta_boxes', 'gffgfg_add_boxes', 999  ); // 999

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多