Getting Multiple Select Box Options to Stay Selected Using Chosen Select
<p>I'm trying to get the selected options to stay selected in the following select box. (The select box resides in the Menus page in WordPress admin). I've verified the data is being saved to the database, but I'm having difficulty figuring out to get the data back from the database to mark the options selected for a given select box after the page is saved. There are multiple select boxes on the page. </p>

<p><a href="https://i.stack.imgur.com/tr5JU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tr5JU.jpg" alt="enter image description here"></a></p>

<p>The select box makes use of the Chosen Select jQuery plugin. Here's a question and answer providing some guidance on marking the options selected with JS: <a href="https://stackoverflow.com/questions/9160988/chosen-select-an-option-by-default-with-jquery">Chosen select an option by default with jQuery</a>. I couldn't get it to work having multiple select boxes on the page. </p>

<pre><code>&lt;script type="text/javascript"&gt;
    jQuery(document).ready(function($){ 
        $(".chzn-select").chosen();
    });
&lt;/script&gt;

&lt;select name="locations[&lt;?php echo $item_id; ?&gt;]" id="edit-menu-item-visibility-&lt;?php echo $item_id; ?&gt;" class="chzn-select" multiple="true"&gt;
    &lt;option value="DZ"&gt;Algeria&lt;/option&gt;
    &lt;option value="AS"&gt;American Samoa&lt;/option&gt;
    &lt;option value="AD"&gt;Andorra&lt;/option&gt;
    ...
</code></pre>

<p>This is how the data gets to the database:</p>

<pre><code>function update_option_text( $menu_id, $menu_item_db_id, $args ) {
    $meta_value = get_post_meta( $menu_item_db_id, 'locations', true );
    $new_meta_value = stripcslashes( $_POST['locations'][$menu_item_db_id] );
    if( '' == $new_meta_value ) {
        delete_post_meta( $menu_item_db_id, 'locations', $meta_value );
    }
    elseif( $meta_value !== $new_meta_value ) {
        update_post_meta( $menu_item_db_id, 'locations', $new_meta_value );
    }
}
</code></pre>

<p>Below is my non-working attempt. If I understand correctly, the values can be set to selected with JS. But I couldn't get that to work either. Any assistance is greatly appreciated. </p>

<pre><code>&lt;option value="AF" &lt;?php $val = implode(",",get_post_meta( $menu_item_db_id, 'locations', true )); if(in_array("AF", $val)){echo "selected='selected'";}else{echo "";} ?&gt; &gt;Afghanistan&lt;/option&gt;
</code></pre>

<p>Here is how the original text input worked:</p>

<pre><code>&lt;input
    type="text" 
    class="widefat code" 
    id="edit-menu-item-visibility-&lt;?php echo $item_id; ?&gt;" 
    name="locations[&lt;?php echo $item_id; ?&gt;]" 
    value="&lt;?php echo esc_html( get_post_meta( $item_id, 'locations', true ) ); ?&gt;" /&gt;
    &lt;?php echo 'Enter 2-digit country codes separated by commas, e.g. US,CA,SG' ?&gt;&lt;/br&gt;
</code></pre>

<p>The entire file can be found <a href="https://gist.github.com/j8d/d81805e76a3816f6aa63" rel="nofollow noreferrer" title="file">here</a>.</p>

<p>UPDATE:</p>

<pre><code>$val = get_post_meta( $item_id, 'locations', true );
$val2 = array_filter(array_map('trim', explode(',', strtoupper(get_post_meta( $item_id, 'locations', true )))));
var_dump($val);
echo "&lt;/br&gt;";   
var_dump($val2);
</code></pre>

<p>Gives <code>string(2) "AD"</code> and <code>array(1) { [0]=&gt; string(2) "AD" }</code>(The value "AD" being one of my selections.)</p>

<p>No matter how many options are selected, it only returns one country code. </p>

<p>UPDATE:</p>

<p>This is what finally worked...</p>

<pre><code>&lt;select name="menu-item-visibility[&lt;?php echo $item_id; ?&gt;][]" id="edit-menu-item-visibility-&lt;?php echo $item_id; ?&gt;" class="chzn-select" multiple="true"&gt;
    &lt;option value="TH" &lt;?php 
                        $val = get_post_meta( $item_id, 'locations', true );
                        if(is_array($val)) {
                            if(in_array("TH", $val)) {
                                echo "selected='selected'";
                            } else {
                                echo "";
                            }
                        } 
                        ?&gt; &gt;Thailand&lt;/option&gt;
</code></pre>