Welcome to my blog, Today we are learning Custome Post Type, Wordpress Hook register_post_type, save_post, update_post_meta Wordpress , get_post_meta, add_meta_boxes
First go to your theme folder and find functions.php and add below code then it will add custom post type
<?php
//past this code in functions.php file
// custom post type for student
function custom_students_post(){
$labels = array(
'name' => _x('Students','Students post type','mywp'),
'singular_name'=> _x('Student','mywp'),
);
$args = array(
'labels' => $labels, // pass here all field label that will display in our post
'description' => __('This is Student content'),
'supports' => array('title','editor','excerpt','author','thumbnail','revisions','custom-fields'),
'taxonomies' => array('category'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
//register post type
register_post_type('students',$args);
}
add_action('init','custom_students_post',0);
//add email meta box
function students_email_metabox(){
add_meta_box(
'emailmetabox456', //this shoud be unique name
'Email Id',
'students_email_meta_show',//callback function this for html view here - > students_email_meta_show
array('students'), //add here custom post type
'normal',
'default',
);
}
add_action('add_meta_boxes','students_email_metabox');
//callback function
function students_email_meta_show($post){
wp_nonce_field(basename(__FILE__), "emailmetaboxnonce");
?>
<label for="email">Email</label>
<input name="email" type="email" value="<?php echo get_post_meta($post->ID, "email", true); ?>">
<?php
}
//save email meta box value
function save_students_email_metabox($post_id){
//this will blank when insert new value
$email_metabox_val = "";
if(isset($_POST["email"]))
{
$email_metabox_val = $_POST["email"];
}
update_post_meta($post_id, "email", $email_metabox_val);
}
add_action('save_post','save_students_email_metabox');
?>
Now go to dashboard and see in left menu you will see "students" section in left side Thank you for visiting my blog
No comments:
Post a Comment
your suggestion are welcome by me