简体   繁体   English

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

[英]How to add custom fields to a WordPress Plugin

I'm being requested by my client to add a custom field that they will be able to enter in a url.我的客户要求我添加一个自定义字段,他们将能够在 url 中输入该字段。 The post itself is a custom plugin custom post type, this is the code I have for this portion:帖子本身是一个自定义插件自定义帖子类型,这是我对这部分的代码:

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;
}

Now the problem is when I go to the post, I do not see the custom meta field even showing up, is there something that I'm missing?现在的问题是,当我去帖子时,我什至没有看到自定义元字段出现,是不是我遗漏了什么?

The custom post type("storylist") comes from the plugin right?自定义帖子类型(“storylist”)来自插件,对吗? Then you don't need to register the custom post again.然后您不需要再次注册自定义帖子。 You just needs to add meta field for this post type and save its value while updating the post.您只需要为此帖子类型添加元字段并在更新帖子时保存其值。 Once I had a experience to enable/disable sidebar using custom field.一旦我有使用自定义字段启用/禁用侧边栏的经验。 I shared my code.我分享了我的代码。 Hope this will help you.希望这会帮助你。

<?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 );


}

?>

Here I have added a custom field called 'post_sidebar' for post type "post" you can change your own and change your post type in this line add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high");在这里,我为帖子类型“帖子”添加了一个名为“post_sidebar”的自定义字段,您可以在此行中更改自己的帖子类型并更改您的帖子类型add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high"); from "post" to "storylist".从“帖子”到“故事列表”。

Just for somebody who needs to add MetaBox fields via plugin , because the question is:仅适用于需要通过plugin添加MetaBox字段的人,因为问题是:

How to add custom fields to a WordPress Plugin如何将自定义字段添加到 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM