Skip to main content

How to create custom meta box inside custom post type in wordpress?

Create custom meta box inside custom post type in wordpress.



<?php

/* custom meta box */

function flipalbums_add_meta_boxes( $post ){
add_meta_box( 'flipalbum_meta_box', __( 'Extras', 'flipalbum_audio_plugin' ), 'flipalbum_audio_meta_box', 'flipalbums', 'normal', 'low' );
}
add_action( 'add_meta_boxes_flipalbums', 'flipalbums_add_meta_boxes' );



function flipalbum_audio_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'flipalbum_meta_box_nonce' );

// retrieve the _food_cholesterol current value
//$current_cholesterol = get_post_meta( $post->ID, '_food_cholesterol', true );

// retrieve the _flip_music_file current value
$current_music = get_post_meta( $post->ID, '_flip_music_file', true );

$customer_email = get_post_meta( $post->ID, '_flip_customer_email', true );

$customer_pin = get_post_meta( $post->ID, '_flip_customer_pin', true );

//$vitamins = array( 'Vitamin A', 'Thiamin (B1)', 'Riboflavin (B2)', 'Niacin (B3)', 'Pantothenic Acid (B5)', 'Vitamin B6', 'Vitamin B12', 'Vitamin C', 'Vitamin D', 'Vitamin E', 'Vitamin K' );

// stores _food_vitamins array
//$current_vitamins = ( get_post_meta( $post->ID, '_food_vitamins', true ) ) ? get_post_meta( $post->ID, '_food_vitamins', true ) : array();

?>
<div class='inside'>

<!--<h3><?php _e( 'Cholesterol', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="radio" name="cholesterol" value="0" <?php checked( $current_cholesterol, '0' ); ?> /> Yes<br />
<input type="radio" name="cholesterol" value="1" <?php checked( $current_cholesterol, '1' ); ?> /> No
</p>-->

<h3><?php _e( 'Music File Goes here', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="text" name="music" value="<?php echo $current_music; ?>" style="width:100%"/>
</p>

<!--<h3><?php _e( 'Vitamins', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<?php
foreach ( $vitamins as $vitamin ) {
?>
<input type="checkbox" name="vitamins[]" value="<?php echo $vitamin; ?>" <?php checked( ( in_array( $vitamin, $current_vitamins ) ) ? $vitamin : '', $vitamin ); ?> /><?php echo $vitamin; ?> <br />
<?php
}
?>
</p>-->
</div>


<div class='inside'>
<h3><?php _e( 'User Email', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="text" name="custemail" value="<?php echo $customer_email; ?>" style="width:100%"/>
</p>


</div>
<div class='inside'>
<h3><?php _e( 'User PIN', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="text" name="custpin" value="<?php echo $customer_pin; ?>" style="width:100%"/>
</p>


</div>

<?php
}


function flipalbums_save_meta_boxes_data( $post_id ){
// verify taxonomies meta box nonce
if ( !isset( $_POST['flipalbum_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['flipalbum_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}

// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}

// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}

// store custom fields values
// cholesterol string
/* if ( isset( $_REQUEST['cholesterol'] ) ) {
update_post_meta( $post_id, '_food_cholesterol', sanitize_text_field( $_POST['cholesterol'] ) );
} */

// store custom fields values
// music string
if ( isset( $_REQUEST['music'] ) ) {
update_post_meta( $post_id, '_flip_music_file', sanitize_text_field( $_POST['music'] ) );
}

if ( isset( $_REQUEST['custemail'] ) ) {
update_post_meta( $post_id, '_flip_customer_email', sanitize_text_field( $_POST['custemail'] ) );
}
if ( isset( $_REQUEST['custpin'] ) ) {
update_post_meta( $post_id, '_flip_customer_pin', sanitize_text_field( $_POST['custpin'] ) );
}

// store custom fields values
/* if( isset( $_POST['vitamins'] ) ){
$vitamins = (array) $_POST['vitamins'];

// sinitize array
$vitamins = array_map( 'sanitize_text_field', $vitamins );

// save data
update_post_meta( $post_id, '_food_vitamins', $vitamins );
}else{
// delete data
delete_post_meta( $post_id, '_food_vitamins' );
} */
}
add_action( 'save_post_flipalbums', 'flipalbums_save_meta_boxes_data', 10, 2 );

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...