How to create a custom post type Step 2
Quote from admin on April 24, 2020, 3:39 pmCreate 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) {}
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) {
}
Quote from admin on April 24, 2020, 3:40 pmPass 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
}
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
}
Quote from admin on April 24, 2020, 3:40 pmSave 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);
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);
Quote from admin on April 24, 2020, 3:41 pmFull 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);
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);