2

Based upon the code found here: How To Add Custom Form Fields To The User Profile Page?

How Could I alter this so that users could check one of several boxes based upon categories I have setup in the blog.

E.g. If I have categories:

Apples Oranges Bananas

Id like these to appear in the User Profile area with a checkbox next to each so that a user can select which fruit they like best.

4

here is an example

//create the user category fields
add_action( 'show_user_profile', 'add_user_categories' );
add_action( 'edit_user_profile', 'add_user_categories' );

function add_user_categories($user ){
    ?>
    <table class="form-table">
    <tr>
        <th><label for="user_categories"><?php _e("User categories"); ?></label></th>
        <td>
            <?php
                $data = get_the_author_meta( 'user_categories', $user->ID );
                $args = array( 'hide_empty' =>0, 'taxonomy'=> 'category');
                $categories=  get_categories($args);
                if ($categories){
                    foreach ( $categories as $category ){ 
                        if(in_array($category->term_id,(array)$data)) {
                            $selected = 'checked="checked""';
                        } else {
                            $selected = '';
                        }
                        echo '<input name="user_categories[]" value="'.$category->term_id.'" '.$selected.' type="checkbox"/>'.$category->name.'<br/>';
                    }
                }
            ?>
        </td>
    </tr>
    </table>
    <?php
}

//save the user category fields 
add_action( 'personal_options_update', 'save_user_categories' );
add_action( 'edit_user_profile_update', 'save_user_categories' );

function save_user_categories( $user_id ){
    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
    update_usermeta( $user_id, 'user_categories', $_POST['user_categories'] );
}

and go get the stored data just use:

$data = get_the_author_meta( 'user_categories', $user->ID );

and $data will be an array with all of the categories the user has selected.

3
  • +1 for the descriptive reply. I would had given the same solution
    – booota
    Sep 14 '11 at 5:38
  • 1
    Hi Bainternet: I used your example but it doesn't seem to save in the database although it saves in the admin section. When I do a search for the newly created user_meta, there are no results. And when I use $data to show the stored data it says "Array."
    – user1893
    Oct 19 '11 at 19:21
  • @Bainternet I know this answer might be a bit old, but I too am stuck with the problem booota is facing. It seems the data is only saving in the admin section, however if I try to put this code on a frontend page, it doesnt load the categories from the array. The checkboxes remain unchecked. I would really appreciate if you can help me with this! Thanks! :)
    – harvey
    Jul 11 '19 at 3:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.