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!


2 responses to “Tip: using WordPress selected() function”

  1. Chris Klosowski

    There’s also one for ‘checked’ for use on checkboxes:

Leave a Reply