我当前的代码基于this question
这是到目前为止我得到的:
// 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()
中放置一个断点时,我没有收到任何要处理的帖子数据。我在做什么错了?