selected( mixed $selected, mixed $current = true, bool $echo = true )

Outputs the HTML selected attribute.


Description Description

Compares the first two arguments and if identical marks as selected


Top ↑

Parameters Parameters

$selected

(mixed) (Required) One of the values to compare

$current

(mixed) (Optional) (true) The other value to compare if not just true

Default value: true

$echo

(bool) (Optional) Whether to echo or just return the string

Default value: true


Top ↑

Return Return

(string) HTML attribute or empty string


Top ↑

Source Source

File: wp-includes/general-template.php

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


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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 2 content
    Contributed by tradesouthwest

    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.

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