functions.php file:
function add_custom_meta_box2()
{
add_meta_box(
'meta-box2',
'SD Extra Page/Post Options ',
'custom_meta_box_markup2',
'page',
'normal',
'low'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box2');
function custom_meta_box_markup1(){
}
function custom_meta_box_markup2($post)
{
global $post;
wp_nonce_field( basename(__FILE__ ), 'custom_meta2_nonce');
$custom_stored_meta2 = get_post_meta( $post->ID, 'sec_title', true );
?>
<div>
<div class="meta-row">
<div class="meta-th">
<label for="sec_title" class="sec-row-title"> Secondary ID</label>
</div>
<div class="meta-td">
<input type="text" name="sec_title" id="sec_title" value="<?php echo esc_attr( $custom_stored_meta2); ?>"/>
</div>
</div>
</div>
<div class="meta">
<div class="meta-th">
<span> Principle Duties</span>
</div>
</div>
<div class="meta-editor">
<?php
$content = get_post_meta($post->ID, 'principle_duties', true);
$editor = 'principle_duties';
$settings = array(
'textarea_rows' => 8,
'media_buttons' => false,
);
wp_editor( $content, $editor, $settings);
?>
</div>
<?php
}
function custom_meta2_save($post_id){
//checks save status
$is_autosave = wp_is_post_autosave( $post_id);
$is_revision = wp_is_post_revision( $post_id);
$is_valid_nonce = ( isset( $POST[ 'custom_meta2_nonce' ] ) && wp_verify_nonce( $_POST['custom_meta2_nonce' ],basename( __FILE__)) ) ? 'true': 'false';
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
//Exit scripts depending on save status
if( $is_autosave || $is_revision || !$is_valid_nonce ){
return;
}
if( isset( $POST['sec_title'] ) ){
update_post_meta( $post_id , 'sec_title', sanitize_text_field( $_POST[ 'sec_title'] ) );
}
if( isset( $POST['principle_duties'] ) ){
update_post_meta( $post_id , 'principle_duties', sanitize_text_field( $_POST[ 'principle_duties'] ) );
}
}
add_action('save_post', 'custom_meta2_save');
functions.php file:
function add_custom_meta_box2()
{
add_meta_box(
'meta-box2',
'SD Extra Page/Post Options ',
'custom_meta_box_markup2',
'page',
'normal',
'low'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box2');
function custom_meta_box_markup1(){
}
function custom_meta_box_markup2($post)
{
global $post;
wp_nonce_field( basename(__FILE__ ), 'custom_meta2_nonce');
$custom_stored_meta2 = get_post_meta( $post->ID, 'sec_title', true );
?>
<div>
<div class="meta-row">
<div class="meta-th">
<label for="sec_title" class="sec-row-title"> Secondary ID</label>
</div>
<div class="meta-td">
<input type="text" name="sec_title" id="sec_title" value="<?php echo esc_attr( $custom_stored_meta2); ?>"/>
</div>
</div>
</div>
<div class="meta">
<div class="meta-th">
<span> Principle Duties</span>
</div>
</div>
<div class="meta-editor">
<?php
$content = get_post_meta($post->ID, 'principle_duties', true);
$editor = 'principle_duties';
$settings = array(
'textarea_rows' => 8,
'media_buttons' => false,
);
wp_editor( $content, $editor, $settings);
?>
</div>
<?php
}
function custom_meta2_save($post_id){
//checks save status
$is_autosave = wp_is_post_autosave( $post_id);
$is_revision = wp_is_post_revision( $post_id);
$is_valid_nonce = ( isset( $POST[ 'custom_meta2_nonce' ] ) && wp_verify_nonce( $_POST['custom_meta2_nonce' ],basename( __FILE__)) ) ? 'true': 'false';
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
//Exit scripts depending on save status
if( $is_autosave || $is_revision || !$is_valid_nonce ){
return;
}
if( isset( $POST['sec_title'] ) ){
update_post_meta( $post_id , 'sec_title', sanitize_text_field( $_POST[ 'sec_title'] ) );
}
if( isset( $POST['principle_duties'] ) ){
update_post_meta( $post_id , 'principle_duties', sanitize_text_field( $_POST[ 'principle_duties'] ) );
}
}
add_action('save_post', 'custom_meta2_save');
Share
Improve this question
edited Mar 13, 2020 at 11:45
Chetan Vaghela
2,4084 gold badges10 silver badges16 bronze badges
asked Mar 12, 2020 at 14:49
Fatima ShakeelFatima Shakeel
114 bronze badges
1 Answer
Reset to default 1You have used $post
instead of $_POST
in save function. That is why data is not saved.
You have should use :
$_POST[ 'custom_meta2_nonce' ]
$_POST['sec_title']
$_POST['principle_duties']
instead of
$POST[ 'custom_meta2_nonce' ]
$POST['sec_title']
$POST['principle_duties']
Use below code to save data :
function custom_meta2_save($post_id){
//checks save status
$is_autosave = wp_is_post_autosave( $post_id);
$is_revision = wp_is_post_revision( $post_id);
$is_valid_nonce = ( isset( $_POST[ 'custom_meta2_nonce' ] ) && wp_verify_nonce( $_POST['custom_meta2_nonce' ],basename( __FILE__)) ) ? 'true': 'false';
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
//Exit scripts depending on save status
if( $is_autosave || $is_revision || !$is_valid_nonce ){
return;
}
if( isset( $_POST['sec_title'] ) ){
update_post_meta( $post_id , 'sec_title', sanitize_text_field( $_POST[ 'sec_title'] ) );
}
if( isset( $_POST['principle_duties'] ) ){
update_post_meta( $post_id , 'principle_duties', sanitize_text_field( $_POST[ 'principle_duties'] ) );
}
}
add_action('save_post', 'custom_meta2_save');
you can use custom page template file or page template file To display data in page:
$sec_title = get_post_meta( get_the_id(), 'sec_title', true );
$principle_duties = get_post_meta(get_the_id(), 'principle_duties', true);
echo "Secondary ID :".$sec_title;
echo "<br>Principle duties :".$principle_duties;