Search code examples
phpwordpresscustom-post-type

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. 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?


Solution

  • The custom post type("storylist") comes from the plugin right? 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"); from "post" to "storylist".