Стоимость мета-боксов Wordpress не сохраняется - PullRequest
2 голосов
/ 19 июня 2020

Я пытаюсь добавить 4 дополнительных поля в качестве содержимого метабокса. Поля правильно отображаются на панели администратора. Однако он не сохраняется, когда я проверял базу данных в таблице wp_postmeta. Кроме того, значение в поле исчезает на стороне администратора после перезагрузки.

Мой код выглядит следующим образом:

загрузка метабоксов

add_action( 'add_meta_boxes','add_badali_property_details_box' );

function add_badali_property_details_box()
{
    add_meta_box(
        'badali-property-details-box',                  // $id
        __('Property Meta Details','badali_real_estate'),  // $title
        'badali_property_details_box_content',      // $callback
        'badali_property',                           // $page
        'normal',                                   // $context
        'high'                                      // $priority
    );
}

function badali_property_details_box_content( $post )
{
    wp_nonce_field( plugin_basename(__FILE__), 'badali_property_details_box_content_nonce' );
    ?>
    <table width="100%">
        <tr>
            <td width="25%"><label for="location">Location:</label></td>
            <td><input type="text" style="min-width:50%"  id="location" name="location" value=""/></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td width="25%"><label for="space">Space (in sqft):</label></td>
            <td><input type="text"  id="space" name="space" value=""/></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td width="25%"><label for="bedroom">Bedroom:</label></td>
            <td ><input type="number"  id="bedroom" name="bedroom" value=""/></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td width="25%"><label for="garage">Garage:</label></td>
            <td><input type="number"  id="garage" name="garage" value=""/></td>
        </tr>
    </table>
    <?php
}

сохранение содержимого метабокса

add_action( 'save_post_badali_property', 'badali_property_details_box_save', 10, 3 );
function badali_property_details_box_save( $post_id )
{
    // verify nonce
    if (!isset($_POST['badali_property_details_box_content_nonce']) || !wp_verify_nonce($_POST['badali_property_details_box_content_nonce'], basename(__FILE__)))
        return 'nonce not verified';

    // check autosave
    if ( wp_is_post_autosave( $post_id ) )
        return 'autosave';

    //check post revision
    if ( wp_is_post_revision( $post_id ) )
        return 'revision';

    // check permissions
    if ( 'badali_property' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) )
            return 'cannot edit page';
    } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
        return 'cannot edit post';
    }

    //so our basic checking is done, now we can grab what we've passed from our newly created form
    $property_data['location'] = $_POST['location'];
    $property_data['space'] = $_POST['space'];
    $property_data['bedroom'] = $_POST['bedroom'];
    $property_data['garage'] = $_POST['garage'];
    update_post_meta( $post_id, 'badali_property', $property_data );

В то время как badali_property - это пользовательский тип сообщения, который я зарегистрировал.

Дайте мне знать, если вам понадобятся дополнительные сведения.

registring_post_type

function badali_init()
{
    $labels = array(
        'name'                  => _x( 'Badali Properties', 'Post type general name', 'badali_real_estate' ),
        'singular_name'         => _x( 'Badali Property', 'Post type singular name', 'badali_real_estate' ),
        'menu_name'             => _x( 'Badali Properties', 'Admin Menu text', 'badali_real_estate' ),
        'name_admin_bar'        => _x( 'Badali Property', 'Add New on Toolbar', 'badali_real_estate' ),
        'add_new'               => __( 'Add New', 'badali_real_estate' ),
        'add_new_item'          => __( 'Add New Property', 'badali_real_estate' ),
        'new_item'              => __( 'New Property', 'badali_real_estate' ),
        'edit_item'             => __( 'Edit Property', 'badali_real_estate' ),
        'view_item'             => __( 'View Property', 'badali_real_estate' ),
        'all_items'             => __( 'All Properties', 'badali_real_estate' ),
        'search_items'          => __( 'Search Properties', 'badali_real_estate' ),
        'parent_item_colon'     => __( 'Parent Properties:', 'badali_real_estate' ),
        'not_found'             => __( 'No properties found.', 'badali_real_estate' ),
        'not_found_in_trash'    => __( 'No properties found in Trash.', 'badali_real_estate' ),
        'featured_image'        => _x( 'Featured Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'badali_real_estate' ),
        'set_featured_image'    => _x( 'Set Featured Image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'badali_real_estate' ),
        'remove_featured_image' => _x( 'Remove Featured Image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'badali_real_estate' ),
        'use_featured_image'    => _x( 'Use As Featured Image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'badali_real_estate' ),
        'archives'              => _x( 'Properties Archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'badali_real_estate' ),
        'insert_into_item'      => _x( 'Insert into properties', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'badali_real_estate' ),
        'uploaded_to_this_item' => _x( 'Uploaded to this property', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'badali_real_estate' ),
        'filter_items_list'     => _x( 'Filter properties list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'badali_real_estate' ),
        'items_list_navigation' => _x( 'Properties list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'badali_real_estate' ),
        'items_list'            => _x( 'Properties list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'badali_real_estate' ),
    );

    $args = array(
        'labels'                => $labels,
        'description'           => 'A custom post type for badali properties',
        'menu_icon'             => 'dashicons-admin-home',
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'query_var'             => true,
        'rewrite'               => array( 'slug' => 'badali_property' ),
        'capability_type'       => 'post',
        'has_archive'           => true,
        'hierarchical'          => false,
        'menu_position'         => null,
        'supports'              => array( 'title', 'editor', 'revisions', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments' ),
        'taxonomies'            => array( 'category','post_tag' ),
        'show_in_rest'          => true
    );

    register_post_type( 'badali_property', $args );
}

обновление

Похоже, что wp_verify_nonce всегда возвращает false. В чем может быть проблема?

...