img Hey! I'm a French WordPress developer! Welcome on my site!
  • Follow me:
Tip: using WordPress selected() function
15th Jun

2012

Tip: using WordPress selected() function

Sometimes we need to save time while coding, and the good news is that WordPress developers team knows that and provides nice little functions to do less code. It’s exactly the purpose of this once: selected(). This function is so simple, but so useful… Here is the official description on the codex:

For use in dropdown form fields. Compares two given values (for example, a saved option vs. one chosen in a form) and, if the values are the same, adds the selected attribute to the current option tag.

So basically it just helps you to make an option selected in a dropdown if the value matches. I told you it’s simple, but i find this so nice because i don’t like wasting time with boring pieces of code. Here is the code provided by the codex to explain how this works:

Just replace the variable to verify and the potential value, and that’s it…. amazing!

https://twitter.com/remicorson/status/213542430509760512

Share This :

Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!

Comments

Chris Klosowski

2013-02-13 15:06:19 Reply

There’s also one for ‘checked’ for use on checkboxes:
http://codex.wordpress.org/Function_Reference/checked

    Rémi

    2013-02-13 17:33:00 Reply

    Absolutely!

Leave a Comment

Your email address will not be published.