0

I am trying to put the option on selected with the built in function of wordpress called selected. I have tried to change the key from opt to different key names for every select field and it still didn't work.

At the moment i am stuck, see my code below:

$taxonomy_args = array(
    'public'    =>  true,
    '_builtin'  =>  false
);

$taxonomy_names = get_taxonomies($taxonomy_args);

$select_field = get_option('select_field');


echo "<form id='example_details' method='post' action''>";
    foreach( $taxonomy_names as $taxonomy_name ) {
        echo "<div class='div_fields'>";
        $taxonomy_name_lc = strtolower($taxonomy_name);
        echo "<label for='${taxonomy_name_lc}_select_field'>${taxonomy_name}</label>";
            echo "<select class='example_form_select' name='details_select_field[opt]' id='${taxonomy_name_lc}_select_field'>";
                echo "<option></option>";
                $terms = get_terms( array( 
                    'hide_empty'    => false,
                    'taxonomy'      => $taxonomy_name
                ) );
                
                foreach ($terms as $term) {
                    echo "<option id='" . $term->name . "' value='" . $term->name . "' " . selected(isset($select_field['opt']), $term->name) . ">" . $term->name . "</option>";
                }


            echo "</select>";
        echo "</div>";
    }
    wp_nonce_field( $this->plugin, 'details_nonce' );
echo "</form>"; 

I did a var_dump on the $select_field variable but that one returns a boolean of false.

  • From your code it seem the option field name is details_select_field[opt] why you are trying to fetch select_field ? – Sabbir Hasan Jun 21 '20 at 14:09
  • Inside the wordpress codex selected(), i did get it from the examples. Do you know a solution to add the atribute selected? Because even when i don’t fetch it’s still not getting selected. But the boolean is returning true now. – Otto Jun 21 '20 at 14:18
  • $select_field = get_option(); get me the var_dump of this. Then I can provide you working code. – Sabbir Hasan Jun 21 '20 at 14:20
  • I need to add an argument inside the function. I am trying to use the get_option function inside my custom metabox but i am now reading that i have to use get_post_meta? Hopefully you can make my wiser. – Otto Jun 21 '20 at 14:23
  • $all_options = wp_load_alloptions(); var_dump($all_options); I doubt your option is being saved. I need to check – Sabbir Hasan Jun 21 '20 at 14:27

Your Answer

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

Browse other questions tagged or ask your own question.