How to Add Meta Boxes for Custom Post Type in WordPress

How-to-Add-Meta-Boxes-for-Custom-post-Type-in-WordPress

This code use for you can add Meta Boses in Custom post Type and this code add your function.php file WordPress.

Custom meta boxes are Mostly used to provide a better user interface for adding custom fields (metadata) into your content.

<?php
function add_service_meta_boxes() {
 add_meta_box("service_contact_meta", "Contact Details", "add_contact_details_service_meta_box", "service", "normal", "low");
}

function add_contact_details_service_meta_box()
{
 global $post;
 $custom = get_post_custom( $post->ID );

 ?>
 <style>.width99 {width:99%;}</style>
 <p>
  <label>Address:</label><br />
  <textarea rows="5" name="address" class="width99"><?= @$custom["address"][0] ?></textarea>
 </p>
 <p>
  <label>Website:</label><br />
  <input type="text" name="website" value="<?= @$custom["website"][0] ?>" class="width99" />
 </p>
 <p>
  <label>Phone:</label><br />
  <input type="text" name="phone" value="<?= @$custom["phone"][0] ?>" class="width99" />
 </p>
 <?php
}


/**
 * Save custom field data when creating/updating posts
 */
function save_service_custom_fields(){
  global $post;

  if ( $post )
  {
    update_post_meta($post->ID, "address", @$_POST["address"]);
    update_post_meta($post->ID, "website", @$_POST["website"]);
    update_post_meta($post->ID, "phone", @$_POST["phone"]);
  }
}
add_action( 'admin_init', 'add_service_meta_boxes' );

add_action( 'save_post', 'save_service_custom_fields' );

?>
Learn Tutorials Point
We Well organized and easy-to-understand Interactive tutorials With lots of examples of how to use Tutorials WordPress, PHP, Bootstrap, Bootstrap 4, HTML, CSS, AJAX, jQuery, Woocommerce, Post, Database, Javascript, Theme, Responsive, Templates.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact us

Fill in the form below or give us a call and we'll contact you. We endeavour to answer all enquiries within 24 hours on business days.