Powered By Blogger

Friday, October 28, 2016

add meta key or meta value or custom field for post

// Post offer box
add_action( 'add_meta_boxes', 'post_offer' );
function post_offer() {
   add_meta_box(
       'post_offer',
       __( 'Price', 'agrg' ),
       'post_offer_content',
       'post',
       'side',
       'high'
   );
}

function post_offer_content( $post ) {
wp_nonce_field( 'myplugin_meta_boxeee', 'myplugin_meta_box_nonceeee' );
$post_offer = get_post_meta( $post->ID, 'post_offer', true );

echo '<label for="post_offer"></label>';
echo '<input type="text" id="post_offer" name="post_offer" placeholder="'._e('Enter price here','agrg').'" value="';
echo $post_offer;
echo '">';

}

add_action( 'save_post', 'post_offer_save' );
function post_offer_save( $post_id ) {

global $post_offer;

if ( ! isset( $_POST['myplugin_meta_box_nonceeee'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonceeee'], 'myplugin_meta_boxeee' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

if(isset($_POST["post_offer"]))
$post_offer = $_POST['post_offer'];
update_post_meta( $post_id, 'post_offer', $post_offer );

}

No comments:

Post a Comment