Discussion:
Saving Radio Options in Plugin Menu
BenderisGreat
2013-11-23 11:07:01 UTC
Permalink
Here is the code, when I select an option, nothing saved. var_dump on
$buttonStyle gives me a boolean false. Feel like I am missing something
here. How exactly should the input be saved? Do I need to validate or
sanitize the input of a radio button? Seems that I wouldnt need to since
its not really user input.

public function ams_choose_comment_buttons_setting() //checkbox input
{
$buttonStyle = get_option('ams_choose_comment_buttons');
var_dump($buttonStyle);

printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteThumbs">voteThumbs', 'ams_choose_comment_buttons', checked(
'voteThumbs' == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteTextLinks">voteTextLinks', 'ams_choose_comment_buttons', checked(
'voteTextLinks' == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteThumbCount">voteThumbCount', 'ams_choose_comment_buttons',
checked( "voteThumbCount" == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteChevrons">voteChevrons', 'ams_choose_comment_buttons', checked(
"voteChevrons" == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="votePlusMinus">votePlusMinus', 'ams_choose_comment_buttons', checked(
"votePlusMinus" == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteLikeDislike">voteLikeDislike', 'ams_choose_comment_buttons',
checked( "voteLikeDislike" == $buttonStyle ));



}




--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Saving-Radio-Options-in-Plugin-Menu-tp42904.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Otto
2013-11-23 11:19:56 UTC
Permalink
No matter how you slice it, this is wrong:

printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteThumbs">voteThumbs', 'ams_choose_comment_buttons', checked(
'voteThumbs' == $buttonStyle ));

You appear to be using the "checked" function to output something that
determines the name, and are also giving all the input boxes the same ID,
which definitely won't work.

Also, yes, they are user input and must be validated/sanitized.


-Otto
Post by BenderisGreat
Here is the code, when I select an option, nothing saved. var_dump on
$buttonStyle gives me a boolean false. Feel like I am missing something
here. How exactly should the input be saved? Do I need to validate or
sanitize the input of a radio button? Seems that I wouldnt need to since
its not really user input.
public function ams_choose_comment_buttons_setting() //checkbox input
{
$buttonStyle = get_option('ams_choose_comment_buttons');
var_dump($buttonStyle);
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteThumbs">voteThumbs', 'ams_choose_comment_buttons', checked(
'voteThumbs' == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteTextLinks">voteTextLinks', 'ams_choose_comment_buttons', checked(
'voteTextLinks' == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteThumbCount">voteThumbCount', 'ams_choose_comment_buttons',
checked( "voteThumbCount" == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteChevrons">voteChevrons', 'ams_choose_comment_buttons', checked(
"voteChevrons" == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="votePlusMinus">votePlusMinus', 'ams_choose_comment_buttons', checked(
"votePlusMinus" == $buttonStyle ));
printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteLikeDislike">voteLikeDislike', 'ams_choose_comment_buttons',
checked( "voteLikeDislike" == $buttonStyle ));
}
--
http://wordpress-hackers.1065353.n5.nabble.com/Saving-Radio-Options-in-Plugin-Menu-tp42904.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
BenderisGreat
2013-11-23 11:23:50 UTC
Permalink
The whole block is wrong? Just for radio buttons or also for checkboxes? I
asked because that works just fine for a checkbox.



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Saving-Radio-Options-in-Plugin-Menu-tp42904p42906.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Otto
2013-11-23 11:36:43 UTC
Permalink
Post by BenderisGreat
The whole block is wrong?
Oh, yes. What is the output from that?

printf(
'<input id="%s" type="radio" name="ams_options[%s]"
value="voteThumbs">voteThumbs', 'ams_choose_comment_buttons', checked(
'voteThumbs' == $buttonStyle ));


The output from that looks like it would probably be the following:

checked='checked'<input id="ams_choose_comment_buttons" type="radio"
name="ams_options[]" value="voteThumbs">voteThumbs

Which simply doesn't make any sort of rational sense.

This is how you would do that correctly. For one, you wouldn't use a printf
at all:

<input type="radio" name="ams_options[ams_choose_comment_buttons]"
value="voteThumbs"
<?php checked($buttonStyle, 'voteThumbs') ?>>voteThumbs</input>

The important things here are:
- No duplicated ID fields. IDs have to be unique across the whole HTML
document. If you want to give them an ID, that's fine, but they cannot all
be the same.
- The "name" must make sense. Sticking the output of the "checked" field
into it makes no sense.
- The "checked" function makes output on its own, calling it as a parameter
inside a printf makes no sense
- You don't need an == check inside the checked() function call.
- The checked function call needs to be made inside the <input> html,
because it's outputting an attribute

-Otto
Otto
2013-11-23 11:46:46 UTC
Permalink
Post by Otto
Post by BenderisGreat
The whole block is wrong?
This is how you would do that correctly. For one, you wouldn't use a
But if you *really* wanted to use a printf, you could do it like this:

printf('<input type="radio" name="ams_options[%s]"
value="voteThumbs"%s>voteThumbs</input>', 'ams_choose_comment_buttons',
checked('voteThumbs', $buttonStyle, false ) );

The third "false" parameter to the checked function suppresses the echo'ing
of the result, then the second %s in the right place in the printf will put
the attribute inside the input html. Also, I added the closing </input> in
there too.

-Otto
BenderisGreat
2013-11-23 11:51:15 UTC
Permalink
Hmm, I actually asked for help here:

http://stackoverflow.com/questions/18675307/checkbox-wont-stay-checked-on-plugin-settings-page

and that was the top upvoted answer so I tried it and it did save the
result. It stored 'on' in the array.

Thank you for clarifying it was wrong. :-/

I was trying to follow your tutorial here :
http://ottopress.com/2009/wordpress-settings-api-tutorial/

but I didnt see anything on radio buttons, and was having trouble with the
validation. I used the code you just helped me with, and it saves properly
but returns:

Notice: Array to string conversion in
/Users/Jared/Documents/Websites/www.local.dev/wp-includes/general-template.php
on line 2343

Now thats what im working on clearing up.



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Saving-Radio-Options-in-Plugin-Menu-tp42904p42909.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Otto
2013-11-23 12:06:55 UTC
Permalink
Post by BenderisGreat
http://stackoverflow.com/questions/18675307/checkbox-wont-stay-checked-on-plugin-settings-page
and that was the top upvoted answer so I tried it and it did save the
result. It stored 'on' in the array.
Thank you for clarifying it was wrong. :-/
Actually, the answer there is mostly correct. He has the duplicated ID
problem, but by using the numbered printf placeholders to duplicate the
first parameter in the printf, he has the values set correctly. Also, he
uses the checked() method correctly, with the false third parameter.

-Otto
BenderisGreat
2013-11-23 12:13:56 UTC
Permalink
Why does the stackoverflow example I pasted store the checked / radio values
but the one you shared does not?



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Saving-Radio-Options-in-Plugin-Menu-tp42904p42911.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
BenderisGreat
2013-11-23 12:51:21 UTC
Permalink
Why does the stackoverflow example I pasted store the checked / radio values
but the one you shared does not? (I mean visually, the radio and
checkboxes show they are ticked, while the example you shared does not store
that value visually. )



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Saving-Radio-Options-in-Plugin-Menu-tp42904p42912.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Otto
2013-11-23 13:22:54 UTC
Permalink
Probably because get_option('ams_choose_comment_buttons'); is incorrect.

It's extremely difficult to give you complete and fully correct answers
when you're only posting snippets. I can only work with what you post. If
you don't post full code, you don't get full answers.

Also, if you need this much help, use the support forums instead of the
mailing list. The list isn't a support forum.


-Otto
Post by BenderisGreat
Why does the stackoverflow example I pasted store the checked / radio values
but the one you shared does not? (I mean visually, the radio and
checkboxes show they are ticked, while the example you shared does not store
that value visually. )
--
http://wordpress-hackers.1065353.n5.nabble.com/Saving-Radio-Options-in-Plugin-Menu-tp42904p42912.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Continue reading on narkive:
Loading...