Title: selected
Published: April 25, 2014
Last modified: August 20, 2026

---

# selected( mixed $selected, mixed $current = true, bool $display = true ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/selected/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/selected/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/selected/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/selected/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/selected/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/selected/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/selected/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/selected/?output_format=md#wp--skip-link--target)

Outputs the HTML selected attribute.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/selected/?output_format=md#description)󠁿

Compares the first two arguments and if identical marks as selected.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/selected/?output_format=md#parameters)󠁿

 `$selected`mixedrequired

One of the values to compare.

`$current`mixedoptional

The other value to compare if not just true.

Default:`true`

`$display`booloptional

Whether to echo or just return the string.

Default:`true`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/selected/?output_format=md#return)󠁿

 string HTML attribute or empty string.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/selected/?output_format=md#source)󠁿

    ```php
    function selected( $selected, $current = true, $display = true ) {
    	return __checked_selected_helper( $selected, $current, $display, 'selected' );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/general-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/general-template.php#L5505)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/general-template.php#L5505-L5507)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/selected/?output_format=md#related)󠁿

| Uses | Description | 
| [__checked_selected_helper()](https://developer.wordpress.org/reference/functions/__checked_selected_helper/)`wp-includes/general-template.php` |

Private helper function for checked, selected, disabled and readonly.

  |

| Used by | Description | 
| [WP_Comments_List_Table::comment_type_dropdown()](https://developer.wordpress.org/reference/classes/wp_comments_list_table/comment_type_dropdown/)`wp-admin/includes/class-wp-comments-list-table.php` |

Displays a comment type drop-down for filtering on the Comments list table.

  | 
| [WP_Posts_List_Table::formats_dropdown()](https://developer.wordpress.org/reference/classes/wp_posts_list_table/formats_dropdown/)`wp-admin/includes/class-wp-posts-list-table.php` |

Displays a formats drop-down for filtering items.

  | 
| [WP_Customize_Nav_Menu_Location_Control::render_content()](https://developer.wordpress.org/reference/classes/wp_customize_nav_menu_location_control/render_content/)`wp-includes/customize/class-wp-customize-nav-menu-location-control.php` |

Render content just like a normal select control.

  | 
| [wp_dropdown_languages()](https://developer.wordpress.org/reference/functions/wp_dropdown_languages/)`wp-includes/l10n.php` |

Displays or returns a Language selector.

  | 
| [install_theme_search_form()](https://developer.wordpress.org/reference/functions/install_theme_search_form/)`wp-admin/includes/theme-install.php` |

Displays search form for searching themes.

  | 
| [WP_List_Table::months_dropdown()](https://developer.wordpress.org/reference/classes/wp_list_table/months_dropdown/)`wp-admin/includes/class-wp-list-table.php` |

Displays a dropdown for filtering items in the list table by month.

  | 
| [mu_dropdown_languages()](https://developer.wordpress.org/reference/functions/mu_dropdown_languages/)`wp-admin/includes/ms.php` |

Generates and displays a drop-down of available languages.

  | 
| [choose_primary_blog()](https://developer.wordpress.org/reference/functions/choose_primary_blog/)`wp-admin/includes/ms.php` |

Handles the display of choosing a user’s primary site.

  | 
| [install_search_form()](https://developer.wordpress.org/reference/functions/install_search_form/)`wp-admin/includes/plugin-install.php` |

Displays a search form for searching plugins.

  | 
| [touch_time()](https://developer.wordpress.org/reference/functions/touch_time/)`wp-admin/includes/template.php` |

Prints out HTML form date elements for editing post or comment publish date.

  | 
| [page_template_dropdown()](https://developer.wordpress.org/reference/functions/page_template_dropdown/)`wp-admin/includes/template.php` |

Prints out option HTML elements for the page templates drop-down.

  | 
| [parent_dropdown()](https://developer.wordpress.org/reference/functions/parent_dropdown/)`wp-admin/includes/template.php` |

Prints out option HTML elements for the page parents drop-down.

  | 
| [media_upload_library_form()](https://developer.wordpress.org/reference/functions/media_upload_library_form/)`wp-admin/includes/media.php` |

Outputs the legacy media upload form for the media library.

  | 
| [post_submit_meta_box()](https://developer.wordpress.org/reference/functions/post_submit_meta_box/)`wp-admin/includes/meta-boxes.php` |

Displays post submit form fields.

  | 
| [WP_Media_List_Table::get_views()](https://developer.wordpress.org/reference/classes/wp_media_list_table/get_views/)`wp-admin/includes/class-wp-media-list-table.php` |

Gets an array of links for the available views on this table.

  | 
| [Custom_Background::admin_page()](https://developer.wordpress.org/reference/classes/custom_background/admin_page/)`wp-admin/includes/class-custom-background.php` |

Displays the custom background page.

  | 
| [wp_dropdown_categories()](https://developer.wordpress.org/reference/functions/wp_dropdown_categories/)`wp-includes/category-template.php` |

Displays or retrieves the HTML dropdown list of categories.

  | 
| [WP_Nav_Menu_Widget::form()](https://developer.wordpress.org/reference/classes/wp_nav_menu_widget/form/)`wp-includes/widgets/class-wp-nav-menu-widget.php` |

Outputs the settings form for the Navigation Menu widget.

  | 
| [WP_Widget_Tag_Cloud::form()](https://developer.wordpress.org/reference/classes/wp_widget_tag_cloud/form/)`wp-includes/widgets/class-wp-widget-tag-cloud.php` |

Outputs the Tag Cloud widget settings form.

  | 
| [WP_Widget_Links::form()](https://developer.wordpress.org/reference/classes/wp_widget_links/form/)`wp-includes/widgets/class-wp-widget-links.php` |

Outputs the settings form for the Links widget.

  | 
| [WP_Widget_Pages::form()](https://developer.wordpress.org/reference/classes/wp_widget_pages/form/)`wp-includes/widgets/class-wp-widget-pages.php` |

Outputs the settings form for the Pages widget.

  | 
| [wp_widget_rss_form()](https://developer.wordpress.org/reference/functions/wp_widget_rss_form/)`wp-includes/widgets.php` |

Displays RSS widget options form.

  | 
| [wp_dropdown_users()](https://developer.wordpress.org/reference/functions/wp_dropdown_users/)`wp-includes/user.php` |

Creates dropdown HTML content of users.

  | 
| [WP_Customize_Control::render_content()](https://developer.wordpress.org/reference/classes/wp_customize_control/render_content/)`wp-includes/class-wp-customize-control.php` |

Renders the control’s content.

  | 
| [wp_print_media_templates()](https://developer.wordpress.org/reference/functions/wp_print_media_templates/)`wp-includes/media-template.php` |

Prints the templates used in the media manager.

  |

[Show 20 more](https://developer.wordpress.org/reference/functions/selected/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/selected/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/selected/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.0.0](https://developer.wordpress.org/reference/since/1.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/selected/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/selected/?output_format=md#comment-content-624)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/selected/#comment-624)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%23comment-624)
     Vote results for this note: 13[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%23comment-624)
 4.  **Example**
 5.      ```php
         <!-- Testing the values with if() -->
         <select name="options[foo]">
         	<option value="1" <?php if ( $options['foo'] == 1 ) echo 'selected="selected"'; ?>>1</option>
         	<option value="2" <?php if ( $options['foo'] == 2 ) echo 'selected="selected"'; ?>>2</option>
         	<option value="3" <?php if ( $options['foo'] == 3 ) echo 'selected="selected"'; ?>>3</option>
         </select>
     
         <!-- Using selected() instead -->
         <select name="options[foo]">
         	<option value="1" <?php selected( $options['foo'], 1 ); ?>>1</option>
         	<option value="2" <?php selected( $options['foo'], 2 ); ?>>2</option>
         	<option value="3" <?php selected( $options['foo'], 3 ); ?>>3</option>
         </select>
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%3Freplytocom%3D624%23feedback-editor-624)
 7.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/selected/?output_format=md#comment-content-3127)
 8.    [tradesouthwest](https://profiles.wordpress.org/tradesouthwestgmailcom/)  [  8 years ago  ](https://developer.wordpress.org/reference/functions/selected/#comment-3127)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%23comment-3127)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%23comment-3127)
 10. **Selected Using foreach loop**
 11.     ```php
         /**
          * Demonstration uses all cpt posts in a dropdown field to select 
          * a featured listings to callback in a function
          */
         function wporg_wpselected_featured_listing_cb()
         {
     
             $post_type = 'wpselected_post';
             $options   = get_option( 'wpselected_lists' );
     
             // select(ed)_id will be a custom option outside of this function.
             $wpselected_select_id = $options['wpselected_featured_listing'];
     
             // Avoid ending up with no string value by setting a default value
             if ( '' === $wpselected_select_id ) {
                 $wpselected_select_id = 0; 
             }
     
             /**
              * Sets up a list of cpt posts to do foreach with
              * @param $label string Optional
              */
             $post_type_object = get_post_type_object( $post_type );
             $label = $post_type_object->label;
             $posts = get_posts( array(
                     'post_type'         => $post_type, 
                     'post_status'       => 'publish', 
                     'suppress_filters'  => false, 
                     'posts_per_page'    => -1
             ) );
             ?>
             <label class="olmin"><?php esc_html_e( 'Listing at top home page.', 'wpselected' ); ?></label>
             <select name="wpselected_lists[wpselected_featured_listing]">
             <option value="0"><?php echo esc_html__( $label, 'wpselected' ); ?></option>
             <?php 
             /**
              * `selected` magic in use
              */
             foreach ( $posts as $post ) {
                 echo '<option value="' . esc_attr( $post->ID ) . '"' 
                      . selected( $wpselected_select_id, $post->ID, false ) . '>' 
                      . esc_html__( $post->post_title, 'wpselected' ) . '</option>';   
             }
             echo '</select>';  
         } 
         ```
     
 12. Ideally you may choose to use `ob_start()` and `ob_get_clean()` to avoid any HTML
     glitches on front.
 13.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%3Freplytocom%3D3127%23feedback-editor-3127)
 14.  [Skip to note 6 content](https://developer.wordpress.org/reference/functions/selected/?output_format=md#comment-content-6136)
 15.   [Chigozie Orunta](https://profiles.wordpress.org/chigozieorunta/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/selected/#comment-6136)
 16. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%23comment-6136)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%23comment-6136)
 17. If you’re going to use this in a sprintf or string function, make sure you turn
     the echo parameter to false (so it returns a string instead of outputting directly)
     like so:
 18.     ```php
         // Defined options
         define( 'OPTIONS',  array( 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D' ) );
     
         // My option
         $my_option  = 1; // OR fetched option value
     
         // Get HTML options
         $html_options = '';
         foreach ( OPTIONS as $key => $value ) {
         	$html_options .= sprintf(
         		'<option value="%1$s" %3$s>%2$s</option>',
         		esc_attr( $key ),
         		esc_html( $value ),
         		selected( $my_option, esc_attr( $key ), false )
         	);
         }
     
         // Display Select element or tag
         echo '<select>' . $html_options . '</select>';
         ```
     
 19.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F%3Freplytocom%3D6136%23feedback-editor-6136)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fselected%2F)
before being able to contribute a note or feedback.