Call me stupid but I coudn’t figure out how to do it. For text input I would just:
<input type="text" name="option_name" value="<?php echo get_option( 'option_name' ); ?>" />
and then hook it into workdpress using register_setting()
. I could then get its value thru get_option('option_name')
. How should I do that with checkboxes and radio buttons?
I tend to store multiple options as an array, so i’d have something like this..
However it does depend how the callback function that sanitizes the incoming data deals with the saved value(the callback you should be defining as the third parameter of
register_setting
). Personally when i’m dealing with checkboxes I don’t set the array key, where as others may choose to set the key to 0(or whatever instead)…So my code actually tends to look like this..
If i’m only dealing with checkboxes my sanitization callback will look something along the lines of..
Ripped that straight from one of my plugin classes(a plugin with only checkbox options), but it’s not code you can expect to work if you copy, it’s there for illustration only..
For radios, if you’re not using multiple selection it goes something like this..
NOTE: It would of course to be wise to check the key is set before comparing against it’s value (i’ve left that out of the above to keep it short).
Did the above help? If not, just let me know what needs clarifying… (or what i’m missing)..
RE:
checked()
You can find where the function is defined(in WordPress) here.
http://core.trac.wordpress.org/browser/tags/3.0.2/wp-includes/general-template.php#L2228
The first parameter is basically a conditional statement, and the second parameter(if you want to define it) is what to check against. The default value to compare against is TRUE… so if were to do
checked( 1 == 1, true )
i’d be checking if 1 == 1 is equal to true. If the conditional hits a match, then you getchecked="checked"
returned to you..NOTE: I’m rubbish at explaining things, so if the above needs further clarification I won’t be offended… just let me know.. 😉
checkbox:
radio: