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

Outputs the HTML selected attribute.

Description

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

Parameters

$selectedmixedrequired
One of the values to compare.
$currentmixedoptional
The other value to compare if not just true.

Default:true

$displaybooloptional
Whether to echo or just return the string.

Default:true

Return

string HTML attribute or empty string.

Source

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

Changelog

VersionDescription
1.0.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Example

    <!-- 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>
  2. Skip to note 5 content

    Selected Using foreach loop

    /**
     * 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>';  
    } 

    Ideally you may choose to use ob_start() and ob_get_clean() to avoid any HTML glitches on front.

  3. Skip to note 6 content

    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:

    // 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>';

You must log in before being able to contribute a note or feedback.