web-dev-qa-db-fra.com

Pourquoi mes entrées de champ de boîte méta personnalisée ne sont-elles pas sauvegardées?

J'ai un type de message personnalisé intitulé "Web_design" et je souhaite que mes messages "Web_design" comportent les champs personnalisés suivants:
1. 'client' - Nom du client
2. 'exlink' - Lien externe
3. 'datefin' - Date de fin

J'ai réussi à insérer une métabox personnalisée (intitulée "Informations sur le travail") dans mon interface utilisateur post-conception "Web Design" avec les entrées nécessaires. Voici le code entier.

<?php
//3. Adding Custom Meta Box for Custom Fields
//a. add custom meta box
function work_meta_box( $post ){
    //variables
    $id = 'work_info';
    $title = 'Work Information';
    $callback = create_work_meta;
    $screen = 'web_design';
    $context = 'side';
    $priority = 'high';
    add_meta_box( $id, $title, $callback, $screen, $context, $priority );
}
add_action( 'add_meta_boxes_web_design', 'work_meta_box' );

//b. callback function for 'work_info' custom meta box (see $callback variable)
function create_work_meta( $post ){
    wp_nonce_field( basename( __FILE__ ), 'work_meta_box_nonce' );
    //custom fields
    $client = get_post_meta( $post->ID, 'client', true );
    $exlink =  get_post_meta( $post->ID, 'exlink', true );
    $datefin = get_post_meta( $post->ID, 'datefin', true );

    echo '<div>
        <p>
            <label for=\'client\'>Client:</label>
            <br />
            <input type=\'text\' name=\'client\' value=\'' . $client .'\' />
        </p>
        <p>
            <label for=\'exlink\'>External Link:</label>
            <br />
            <input type=\'url\' name=\'exlink\' value=\''. $exlink . '\' />
        </p>
        <p>
            <label for=\'datefin\'>Date Finished:</label>
            <br />
            <input type=\'date\' name=\'datefin\' value=\''. $datefin . '\' required />
        </p>
    </div>';
}

//c. saves data after submitting/updating custom post
function save_work_meta( $post_id ){
    //1. verifies meta box nonce (to prevent CSRF attacks)
    if( !isset( $_POST['work_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['work_meta_box_nonce'] ) ){
        return;
    }
    //2. if autosaves
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
        return;
    }
    //3. if user's not admin
    if( !current_user_can( 'edit_post', $post_id ) ){
        return;
    } elseif ( !current_user_can ( 'edit_page', $post_id) ){
        return;
    }
    //4. checks all custom field values (see 'create_work_meta()' function)
    if( isset( $_REQUEST['client'] ) ){
        update_post_meta( $post_id, 'client', sanitize_text_field( $_POST['client'] ) );
    }
    if( isset( $_REQUEST['exlink'] ) ){
        update_post_meta( $post_id, 'exlink', sanitize_text_field( $_POST['exlink'] ) );
    }
    if( isset( $_REQUEST['datefin'] ) ){
        update_post_meta( $post_id, 'datefin', sanitize_text_field( $_POST['datefin'] ) );
    }
}
add_action( 'save_post_web_design', 'save_work_meta', 10, 2 );
?>

Voici un code supplémentaire pour la création du type de publication 'web_design'. Les paramètres que j'ai utilisés peuvent indiquer comment cela fonctionnera.

// H. Creating Custom Posts
// 1. 'Web Design' Posts (label: 'web_design')
function web_design_init(){
    //a. array set for '$labels' variable
    $labels = array(
        'name' => __( 'Web Design' ),
        'add_new' => __( 'Add New Sample' ),
        'add_new_item' => __( 'Web Design Sample'),
        'edit_item' => __( 'Edit Sample' ),
        'new_item' => __( 'New Sample' ),
        'search_items' => __( 'Search Sample' ),
        'not_found' => __( 'No Samples Found' ),
        'not_found_in_trash' => __( 'No Samples to Recover' )
    );

    //b. array set for '$supports' variable
    $supports = array(
        'title',
        'editor',
        'thumbnail',
        'post-formats'
    );

    //c. array set for '$args' variable
    $args = array(
        'labels' => $labels, //a.
        'description' => 'For web design works in portfolio.',
        'public' => true,
        'capability_type' => 'post', // <---- I want the 'web_design' posts to have the same capabilities as a default post item.
        'menu_position' => 5,
        'menu_icon' => 'dashicons-images-alt',
        'hierarchal' => false,
        'has_archive' => true,
        'supports' => $supports, //b.
        'taxonomies' => array('post_tag')
    );
    //registers 'web_design' post type
    register_post_type( 'web_design', $args );
}
add_action( 'init', 'web_design_init' );
1
Migs M.

Vous ne vérifiez pas le nonce correctement. Essayez ceci, ça devrait marcher:

//1. verifies meta box nonce (to prevent CSRF attacks)
if( !isset( $_POST['work_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['work_meta_box_nonce'], basename( __FILE__ ) ) ){
        return;
}

Pour plus d'informations sur wp_verify_nonce, voir ici .

0
websupporter