Adding Extra Fields to the WordPress User Profile: Checkboxes

C.Bavota wrote a nice tutorial on adding user profile fields to the WordPress database by modifying the functions.php file in your WordPress theme.  I followed his article with success; however, I ran into difficulty when adding checkboxes.  Here is my addition to his good code.

To use checkboxes, you need to do two things:
  1. First, you need to set a “value” for the checkbox input element (I use value=”yes”).
  2. Second, you need to appropriately set the “checked” attribute of the <input> element.
See the code below.
<input type="checkbox" name="my_field" id=" my_field " value="yes" <?php if (esc_attr( get_the_author_meta( "my_field", $user->ID )) == "yes") echo "checked"; ?> /><label for="my_field "><?php _e("My Field"); ?></label><br />
The code above queries the current value of the field from the database; if it should be checked, then the PHP will write the “checked” attribute into the <input> element.  The resulting HTML looks like this:
<input type="checkbox" name="my_field" id=" my_field" value="yes" checked class="regular-text" />
Some websites recommended using checked=”checked”; I tried that, and it seemed to always leave the checkbox checked.  If you figure it out, please let me know, since I believe that is the “correct” way to do it, per W3C Recommendation.
The code to update checkbox fields is the same for textbox fields (C.Bavota’s tutorial):
update_user_meta( $user_id, 'my_field', $_POST['my_field'] );
Happy coding!
James
Update 30 Mar 2012: This code is for adding checkbox fields to the user profile page.  You may want to add some of these fields to the registration form.  To do that, I use Theme My Login, following Jeff Farthing’s example code.  In sort, it involves editing two PHP files: one in your theme directory and one in the plugins directory.
Advertisements

3 comments on “Adding Extra Fields to the WordPress User Profile: Checkboxes

  1. […] what if you need to use different form fields, like a checkbox? This tutorial by James Carroll covers how to set up a simple checkbox; I’ve recreated the first snippet of code used above […]

  2. laws says:

    i was looking for this and yours is the first blog post i came across that actually told me how to do it, and furthermore it worked.

    (PS one note, your code on the page is breaking across to your sidebar, I recommend adding code between the

    
    

    or tags in your posts.

    cheers
    loz

  3. Rudolf says:

    with this code the update button will work even if nothing is changed:

    update_user_meta( $user_id, ‘hide_lehrer’, isset($_POST[‘hide_lehrer’]) ? “yes” : null );

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s