• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Content Modeling for WordPress
    • WordPress Hidden Gems
    • Web Design
  • Work
    • Presentations and Interviews
    • on GitHub →
    • MLIS Class Projects (2019-2022)
    • Portfolio (2002-2019)
    • WordPress Plugins

Stephanie Leary

Writer, Front End Developer, former WordPress consultant

  • About
    • Press Kit
    • Presentations and Interviews
  • Contact Me

WordPress Hidden Gems: checked(), selected(), and disabled()

September 22, 2010 Stephanie Leary 5 Comments

This one’s for developers! If you’re using checkboxes, radio buttons, or dropdowns in your theme or plugin options, you might have overlooked some very useful functions that aren’t listed in the Codex: checked(), selected(), and disabled(). These allow you to compress the code required to see whether or not the relevant option has already been selected, or whether the form option should be disabled. This results in much more readable code.

// Testing the value with if()
<input type="checkbox" checked="checked" name="options[postlink]" value="1" /> />

// Using checked() instead
<input type="checkbox" checked="checked" name="options[postlink]" value="1" /> />

Checked() works with radio buttons as well as checkboxes.

In a long list of dropdown options, the difference with selected() is even more striking.

// Testing the value with if()
<select name="options[content]"><option title="" selected="selected" value="title">>Title Only</option><option selected="selected" value="excerpt">>Title and Excerpt</option><option value="content"> echo 'selected="selected"'; ?>>Title and Content</option>
</select>
// Using selected() instead
<select name="options[content]"><option title="" selected="selected" value="title">>Title Only</option><option selected="selected" value="excerpt">>Title and Excerpt</option><option selected="selected" value="content">>Title and Content</option>
</select>

The third function, disabled(), is used to disable a button if the two given values match. Here’s an example from the core:

// using if()
<input id="hostname" type="text" name="hostname" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo " />"<!--?php if ( defined('FTP_HOST') ) echo ' disabled="disabled"' ?--> size="40" />
// using disabled() instead
<input id="hostname" type="text" name="hostname" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo " />"<!--?php disabled( defined('FTP_HOST') ); ?--> size="40" />

Find all the WordPress Hidden Gems in WordPress for Web Developers.

WordPress

This is an excerpt from Content Strategy for WordPress.My latest books are Content Strategy for WordPress (2015) and WordPress for Web Developers (2013). Sign up to be notified when I have a new book for you.

Reader Interactions

Comments

  1. Barrett Golding says

    September 23, 2010 at 8:01 am

    Thanks loads for this. Put it to immediate use.

    Your checked() above might need a minor correction tho: there’s a black space before the php (after the value=).

    That space isn’t needed, as it’s conditionally added by WP’s __checked_selected_helper(). If match is true, that function returns: " $type='$type'" (where $type equals 'selected', 'checked' or 'disabled'). So that space will be writ if needed.

    Your selected() and disabled() examples above, correctly, do not have that space.

    Again, thanks much for this, and the other hidden gems you’ve revealed.

    Reply
  2. Prasad says

    September 23, 2010 at 8:11 am

    These were really unknown (at least to me really)…thanks for sharing..!!

    Reply
  3. kathy says

    December 30, 2010 at 2:42 pm

    wow- i’ve wasted quite a bit of time doing that the old way.

    Reply
  4. xiangzi says

    September 24, 2012 at 11:03 pm

    you should use it like this

    checked(1,$options['postlink']);

    Reply
  5. Morten says

    July 29, 2015 at 5:24 am

    I don’t get it. Isn’t there something wrong with those lines of code? You don not seem to be using either of the functions mentioned…

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Footer

My Books

I’m a front end developer at Equinox OLI, working on open source library software. I was previously a freelance WordPress developer in higher education. You can get in touch here or on LinkedIn.

Copyright © 2025 Stephanie Leary · Contact