Stephanie Leary

Writer and WordPress consultant

  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Fascism Watch (2016-17)
    • Content Modeling for WordPress series
    • WordPress Hidden Gems series
  • Work
    • Portfolio
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →
  • About
    • Press Kit
  • Contact
    • Mailing List

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

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 *

Latest WordPress Book

Content Strategy for WordPress

A short book for content strategists and managers on implementing a complete content strategy in WordPress: evaluation, analysis, content modeling, editing and workflows, and long-term planning and maintenance.

Read the sample chapter

Kindle Nook iBooks Kobo Smashwords

WordPress for Web Developers

WordPress for Web Developers (9781430258667)

This is a book for professional web designers and developers who already know HTML and CSS, and want to learn to build sites with WordPress. The book begins with a detailed tour of the administration screens and settings, then digs into server-side topics like performance and security. The second half of the book is devoted to development: learning to build WordPress themes and plugins.

This is the second, much-revised and updated edition of Beginning WordPress 3, with a more accurate title. Everything’s been updated for WordPress 3.6.

WordPress for Web Developers is out now. See what's inside...

The best WordPress features you’ve never noticed

  • WordPress Hidden Gems: Screen Options
  • WordPress Hidden Gems: Bulk Edit
  • WordPress Hidden Gems: Private Status
  • WordPress Hidden Gems: Dashboard Feed Readers
  • WordPress Hidden Gems: Options.php

Content Modeling for WordPress series

  • Content modeling for WordPress, part 1: analyze content
  • Content modeling for WordPress, part 2: functional and organizational requirements
  • Content modeling for WordPress, part 3: a sample content model

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.

Copyright © 2022 Stephanie Leary