如何保存元框值?

Rif*_*arr 0 wordpress custom-fields meta-boxes

我创建了一个元框。代码是:

// Create your custom meta box
add_action( 'add_meta_boxes', 'hotel_amenities' );
   // Add a custom meta box to a post
    function hotel_amenities( $post ) { 
        add_meta_box(
                'Meta Box Amenities', // ID, should be a string
                'Amenities', // Meta Box Title
                'amenities_content', // Your call back function, this is where your form field will go
                'post', // The post type you want this to show up on, can be post, page, or custom post type
                'normal', // The placement of your meta box, can be normal or side
                'high' // The priority in which this will be displayed
            ); 
    }

    // Content for the custom meta box
    function amenities_content( $post ) { 
        echo '<label>Bed room</label>'; 
        echo '<input type="text" name="amenity_bed_room" value="" />'; 
    }

    // Save your meta box content
    add_action( 'save_post', 'save_amenities' );
    // save newsletter content
    function save_amenities(){ 
        global $post; 
        // Get our form field
        if( $_POST ) : 
             $amenities_meta = esc_attr( $_POST['amenity_bed_room'] ); 
             // Update post meta
             update_post_meta($post->ID, '_amenities_custom_meta', $amenities_meta); 
        endif; 
    }
Run Code Online (Sandbox Code Playgroud)

它在管理员帖子页面上显示带有文本字段的元框。但是如果我在文本字段中放入一些内容后保存或更新帖子,该内容将变为空白。

屏幕截图

似乎function save_amenities()无法正常工作。我在这段代码中做错了什么?

为了获得该值,我使用下面的函数。那是对的吗?

//get amenities meta box values
function get_amenities_meta_box() {
    global $post;
    $meta_values = get_post_meta($post->ID, '_amenities_custom_meta', true); 
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*sar 5

那里有些错误。您要查看的最终值将通过amenities_content函数中的value属性显示。现在,它只是显示一个空字符串(“”)。尝试在该属性中放置任何值,您应该会看到它显示在元框(value="this is a test")中。

save_amenities功能应$post_id作为参数。您将需要更新后的元数据,并为该amenities_content函数提供真实值以回显至管理屏幕。

amenities_content函数实际上应该具有一个随机数字段,然后应由该save_amenities函数进行验证。用户输入在保存之前应进行清理(保存和显示时都在进行此操作。我不确定是否有必要。)

试试这个amenities_content功能:

function amenities_content( $post ) {
    // This is the value that was saved in the save_amenities function
    $bed_room = get_post_meta( $post->ID, '_amenity_bed_room', true );

    wp_nonce_field( 'save_amenity', 'amenity_nonce' );

    echo '<label>Bed room</label>';
    echo '<input type="text" name="amenity_bed_room"
        value="' . sanitize_text_field( $bed_room ) . '" />';
}
Run Code Online (Sandbox Code Playgroud)

这对于save_amenities功能:

function save_amenities( $post_id ) {

    // Check if nonce is set
    if ( ! isset( $_POST['amenity_nonce'] ) ) {
        return $post_id;
    }

    if ( ! wp_verify_nonce( $_POST['amenity_nonce'], 'save_amenity' ) ) {
        return $post_id;
    }

    // Check that the logged in user has permission to edit this post
    if ( ! current_user_can( 'edit_post' ) ) {
        return $post_id;
    }

    $bed_room = sanitize_text_field( $_POST['amenity_bed_room'] );
    update_post_meta( $post_id, '_amenity_bed_room', $bed_room );
}
Run Code Online (Sandbox Code Playgroud)