I am in the process of building my first WordPress plugin. Of course I am wildly researching how to do things. One thing that was particularly hard to find was how to use checkboxes in options pages. Here is the solution I used.
WordPress has a function called checked(). This basically returns a true checked attribute if the conditions it is passed are met. Here is how I used it:
<input type="checkbox" name="my_options" <?php checked( get_option('my_option') == 'on',true); ?> />
What I found is that when the option was getting updated it was being stored as 'on'. So this little PHP snippet says: "If the option named 'my_option' is equal to 'on' then add a checked="checked" attribute to this input tag." Anyway, I found it quite hard to get a straight up answer to this problem. Since the reason I made this blog is to share my discoveries; here you go.
James Doyle
I'm a full-stack developer, co-organizer of PHP Vancouver meetup, and winner of a Canadian Developer 30 under 30 award. I'm a huge Open Source advocate and contributor to a lot of projects in my community. When I am not sitting at a computer, I'm trying to perfect some other skill.