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 use inner html value or data in php from javascript(innerHTML)?

 use inner html value or data in php from javascript(innerHTML)? <html> <body> <p id="demo">use inner html value in php(innerhtml)</p> <script>   var jassi = document.getElementById("demo").innerHTML;   //document.write(jassi); </script> <?php $jassi = '<script>document.write(jassi);</script>'; echo $jassi; ?> </body> </html>

How to get store data from WCFM(Best Multi Vendor Marketplace) Plugin?

 Get store data from WCFM(Best Multi Vendor Marketplace) Plugin. global $WCFM, $WCFMmp, $wp, $WCFM_Query, $post; $store_id = ''; if ( isset( $attr['id'] ) && !empty( $attr['id'] ) ) { $store_id = absint($attr['id']); } if (  wcfm_is_store_page() ) { $wcfm_store_url = get_option( 'wcfm_store_url', 'store' ); $store_name = apply_filters( 'wcfmmp_store_query_var', get_query_var( $wcfm_store_url ) ); $store_id  = 0; if ( !empty( $store_name ) ) { $store_user = get_user_by( 'slug', $store_name ); } $store_id    = $store_user->ID; } $user_id = $store_id;  $vendor_data = get_user_meta( $user_id, 'wcfmmp_profile_settings', true ); $street_1 = $vendor_data['address']['street_1']; $street_2 = $vendor_data['address']['street_2']; $city     = $vendor_data['address']['city']; $zip      = $vendor_data['address']['zip&#

How to create login with phone number in woocommerce wordpress?

Create login with phone number in woocommerce wordpress. function wooc_extra_register_fields() {?>        <p class="form-row form-row-wide">        <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>        <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />        </p>            <?php  }  add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );  function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) { if ( isset( $_POST['billing_phone'] ) ) {    $hasPhoneNumber= get_users('meta_value='.$_POST['billing_phone']);    if ( !empty($hasPhoneNumber)) {      $validation_er