How to add Extra Input Field to Wordpress Posts

How to add Extra Input Field to Wordpress Posts?

Registering Price meta box for your blog post you can use this code.

add_action( 'add_meta_boxes', 'price_meta_box_add' );
function price_meta_box_add() {
    add_meta_box( 'price-meta-box-id', 'Put Price Here!', 'price_meta_box_cb', 'post', 'side', 'high' );
function price_meta_box_cb() {
    wp_nonce_field( basename( __FILE__ ), 'price_meta_box_nonce' );
    $value = get_post_meta(get_the_ID(), 'price_key', true);
    $html = '<label>Price: </label><input type="text" name="price" value="'.$value.'"/>';
    echo $html;
add_action( 'save_post', 'price_meta_box_save' );
function price_meta_box_save( $post_id ){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( !isset( $_POST['price_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['price_meta_box_nonce'], basename( __FILE__ ) ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;
    if( isset( $_POST['price'] ) )
        update_post_meta( $post_id, 'price_key', esc_attr( $_POST['price'], $allowed ) );

to output that field in your post page single.php you must use this code to output. Make sure you put this code in loop if(have_post) while loop ...
<?php get_post_meta(get_the_ID(), 'price_key', true); ?>

Read Full Tutorial here Click here to visit the full tutorial

Post a Comment