0

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;

}
0

First of all, You don't have to add 'form' html tag in the metabox.

You missed to pass the '$post' parameter to the function 'display_post_options' and you used selected() function wrong too. Here is the code after I updated it.

function display_post_options( $post){

wp_nonce_field( basename( __FILE__ ), 'post_options_nonce' ); 
$post_options_select_value =  get_post_meta($post->ID,'post_options_select',true);

?>


Choose Your Post Layout:
<select name='post_options_select'>
<option id='right-sidebar' value='right' name='right_sidebar' <?php selected($post_options_select_value, 'right'); ?> >Right sidebar</option>
<option id='left-sidebar' value="left" name='left_sidebar' <?php selected($post_options_select_value, 'left'); ?>>Left Sidebar</option>
<option id='no-sidebar' value="no" name='no_sidebar' <?php selected($post_options_select_value, 'no'); ?>>No Sidebar</option>
</select>

You actually need to pass the value of the meta not the key in order to 'selected ()' function work properly.

Also you need to change this line :

add_action( 'save_post', 'save_post_options', 10, 2 );

to

add_action( 'save_post', 'save_post_options', 10 );

In your case, you need just one parameter to be passed to 'save_post_options' function.

I hope this would be helpful.

  • Still not working.When i select no sidebar and update the post it doesn't show no sidebar in select list. – user3443160 Aug 26 '14 at 3:22
  • I figured it out.I forgot to call the global variable $post in the function. Now its working fine.Thanks for the help i appreciate it :) – user3443160 Aug 26 '14 at 3:27
  • now it is outputting something like this " selected='selected'Choose Your Post Layout: (the select list)" what i did is just removed those php ending tags where my html resides and i assing my html to a variable.YOu can see it in my question. – user3443160 Aug 26 '14 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.