Warning: Cannot modify header information - headers already sent by (output started at /home/n742ef5/public_html/forum/index.php:3) in /home/n742ef5/public_html/forum/wp-content/plugins/asgaros-forum/includes/forum-online.php on line 80

Warning: Cannot modify header information - headers already sent by (output started at /home/n742ef5/public_html/forum/index.php:3) in /home/n742ef5/public_html/forum/wp-content/plugins/asgaros-forum/includes/forum-unread.php on line 43

Warning: Cannot modify header information - headers already sent by (output started at /home/n742ef5/public_html/forum/index.php:3) in /home/n742ef5/public_html/forum/wp-content/plugins/asgaros-forum/includes/forum-unread.php on line 82
How to create a custom post type Step 2 – Sithtamil – My Notes

Forum

Please or Register to create posts and topics.

How to create a custom post type Step 2

Create Meta Box

I need to add two text box call producer name and email ID

first I need to add meta box

// meta box registration
function wpl_owt_cpt_register_metabox() {

add_meta_box("cpt-id", "Producer Details", "wpl_owt_cpt_producer_call", "movie", "side", "high");
}
add_action("add_meta_boxes", "wpl_owt_cpt_register_metabox");

// meta function call
function wpl_owt_cpt_producer_call($post) {

}

Pass the values

function wpl_owt_cpt_producer_call($post) {
?>
<p>
<label>Name:</label>
<?php $name = get_post_meta($post->ID, "wpl_producer_name", true) ?>
<input type="text" value="<?php echo $name; ?>" name="txtProducerName" placeholder="Name"/>
</p>
<p>
<label>Email:</label>
<?php $email = get_post_meta($post->ID, "wpl_producer_email", true) ?>
<input type="email" value="<?php echo $email; ?>" name="txtProducerEmail" placeholder="Email"/>
</p>
<?php
}

Save and update the valuses

// Save and update meta
function wpl_owt_cpt_save_values($post_id, $post) {

$txtProducerName = isset($_POST['txtProducerName']) ? $_POST['txtProducerName'] : "";
$txtProducerEmail = isset($_POST['txtProducerEmail']) ? $_POST['txtProducerEmail'] : "";

update_post_meta($post_id, "wpl_producer_name", $txtProducerName);
update_post_meta($post_id, "wpl_producer_email", $txtProducerEmail);
}

add_action("save_post", "wpl_owt_cpt_save_values", 10, 2);

Full code for meta box registration

// meta box registration
function wpl_owt_cpt_register_metabox() {

add_meta_box("cpt-id", "Producer Details", "wpl_owt_cpt_producer_call", "movie", "side", "high");
}
add_action("add_meta_boxes", "wpl_owt_cpt_register_metabox");

// meta function call
function wpl_owt_cpt_producer_call($post) {
?>
<p>
<label>Name:</label>
<?php $name = get_post_meta($post->ID, "wpl_producer_name", true) ?>
<input type="text" value="<?php echo $name; ?>" name="txtProducerName" placeholder="Name"/>
</p>
<p>
<label>Email:</label>
<?php $email = get_post_meta($post->ID, "wpl_producer_email", true) ?>
<input type="email" value="<?php echo $email; ?>" name="txtProducerEmail" placeholder="Email"/>
</p>
<?php
}

// Save and update meta
function wpl_owt_cpt_save_values($post_id, $post) {

$txtProducerName = isset($_POST['txtProducerName']) ? $_POST['txtProducerName'] : "";
$txtProducerEmail = isset($_POST['txtProducerEmail']) ? $_POST['txtProducerEmail'] : "";

update_post_meta($post_id, "wpl_producer_name", $txtProducerName);
update_post_meta($post_id, "wpl_producer_email", $txtProducerEmail);
}

add_action("save_post", "wpl_owt_cpt_save_values", 10, 2);