WordPress пользовательский метабокс не отправляет данные поста - PullRequest
0 голосов
/ 21 января 2019

Мой текущий код основан на этом вопросе

Это то, что у меня так далеко:

// Add custom Slider ID field to 'Edit Page'
add_action( 'add_meta_boxes', function () {
    add_meta_box( 'menu-selector', 'Menu Selector', 'menu_selector_cb', 'page', 'side', 'high' );
});

function menu_selector_cb( $post ) {
    $values = get_post_custom( $post->ID );
    $text = isset( $values['selected_menu'] ) ? esc_attr( $values['selected_menu'][0] ) : '';
    wp_nonce_field( 'my_menu_selector_nonce', 'menu_selector_nonce' );
    ?>
    <p>
        <label for="selected-menu">Select a menu</label>
        <input type="text" name="selected_menu" id="selected-menu" value="<?php echo $text; ?>" />
        <input type="submit">
    </p>
    <?php
}

add_action( 'save_post', 'menu_selector_save', 10, 2 );
function menu_selector_save( $post_id ) {
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['menu_selector_nonce'] ) || !wp_verify_nonce( $_POST['menu_selector_nonce'], 'my_menu_selector_nonce' ) ) return;
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post', $post_id ) ) return;
    // now we can actually save the data
    $allowed = array(
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );
    // Probably a good idea to make sure your data is set
    if( isset( $_POST['menu_selector_text'] ) )
        update_post_meta( $post_id, 'selected_menu', wp_kses( $_POST['selected_menu'], $allowed ) );
}

У меня проблемы с сохранением. Когда я нажимаю «обновить» и устанавливаю точку останова в menu_selector_save(), я не получаю никаких данных для обработки. Что я делаю не так?

...