I want to update my select options(drop down) but i don't know where i made a mistake.Here is my code.
<?php
function display_post_options(){
wp_nonce_field( basename( __FILE__ ), 'post_options_nonce' );
?>
<form action='' id='post-options-form' method='post'>
Choose Your Post Layout:
<select name='post_options_select'>
<option id='right-sidebar' value='right' name='right_sidebar' <?php selected('post_options_select', 'right'); ?> >Right sidebar</option>
<option id='left-sidebar' value="left" name='left_sidebar' <?php selected('post_options_select', 'left'); ?>>Left Sidebar</option>
<option id='no-sidebar' value="no" name='no_sidebar' <?php selected('post_options_select', 'no'); ?>>No Sidebar</option>
</select>
</form>
<?php
}
function post_options(){
add_meta_box(
'post-options',
'Post Options',
'display_post_options',
'post',
'advanced',
'high',
$callback_args
);
}
function save_post_options($post_id){
if (if_user_can_save($post_id, 'post_options_nonce')) {
update_post_meta($post_id, 'post_options_select', $_POST['post_options_select']);
}
}
function if_user_can_save($post_id, $nonce){
//Check if post is autosave
$is_autosave = wp_is_post_autosave($post_id);
//Check if it is a Revision
$is_revision = wp_is_post_revision($post_id);
//Is the nonce valid
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], basename( __FILE__ ) ) );
return !($is_autosave || $is_revision) && $is_valid_nonce;
}
add_action('add_meta_boxes', 'post_options');
add_action( 'save_post', 'save_post_options', 10, 2 );
?>
UPDATE:(See comment #3) This is where i assigned my html to a variable.But it outputs (selected='selected'Choose Your Post Layout: [the select list])
function display_post_options(){
global $post;
wp_nonce_field( basename( __FILE__ ), 'post_options_nonce' );
$post_options_select_value = get_post_meta($post->ID,'post_options_select',true);
$html = 'Choose Your Post Layout: ';
$html .= '<select name="post_options_select">';
$html .= '<option id="right-sidebar" value="right" name="right_sidebar"' . selected($post_options_select_value, 'right') . '">Right sidebar</option>';
$html .= '<option id="left-sidebar" value="left" name="left_sidebar"' . selected($post_options_select_value, 'left') . '" >Left Sidebar</option>';
$html .= '<option id="no-sidebar" value="no" name="no_sidebar"' . selected($post_options_select_value, 'no') . '">No Sidebar</option>';
$html .= '</select>';
echo $html;
}