0

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');

1 Answer 1

0

I had the same problem and solved it by replacing

if( !current_user_can( 'edit_post' ) ) return;

by:

if ( !current_user_can('edit_post', $post_id) ) return;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.