wp admin – Wp doesn’t save meta box data

Question

My WP doesn’t save my meta box data. I see the metabox and the field, but the data doesn’t saving. Somebody could help me? Thank you. This is the code:

// 1. Metabox létrehozása
function my_meta_box(){
    add_meta_box('subtext', 'My meta box', 'my_html', 'page', 'normal', 'high');
}
add_action('add_meta_boxes', 'my_meta_box');

// 2. Metabox HTML kimenet a doboz tartalmi részében
function my_html($post){
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' ); // rejtett mezők beolvasása
    $subtext = get_post_meta($post->ID, 'subtext', true); 
    echo '<p>';
    echo '<label class="post-attributes-label">Mező cím</label><br/>';
    echo '<input type="text" name="subtext" value="'. $subtext .'">';
    echo '</p>';
}

// 3. bevitt adatok mentése
function my_save_postdata($post_id){
    // Auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
 
    // Authorization 
    if ( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    
    // User's permissions
    if( !current_user_can( 'edit_post' ) ) return;
    
    // Fields
    if (array_key_exists('subtext', $_POST)) {
        update_post_meta($post_id, 'subtext', $_POST['subtext']);
    }
}
add_action('save_post', 'my_save_postdata');

0
t31os 2 weeks 2022-11-13T13:08:12-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse