Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 335
MikeL5799 Avatar asked Feb 11 '26 13:02

MikeL5799


1 Answers

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

like image 95
Palanivelrajan Avatar answered Feb 14 '26 04:02

Palanivelrajan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!