How to add/save Custom Field in user settings/profile “Checkbox list”

i have been working on a custom system for my site where users can have a seperate account, setting and profile page to make my site a bit more interactive and community type. with some other additional stuff.
I am new to coding, i have managed to make the profile pages etc.

Right now i am stuck in adding a check box list of tags to user settings page. where a user can select multiple tags as interest.. so i can use those to show him recommended posts randomly on his account page .

i was able to add two extra fields (Twitter/facebook) in the settings page with this code :

function add_bkmrks_contactmethod( $bcontactmethods ) {
$bcontactmethods['Twitter'] = 'Twitter';
$bcontactmethods['Facebook'] = 'Facebook';
return $bcontactmethods;
}
add_filter('user_contactmethods','add_bkmrks_contactmethod',10,1);

and tehse can be easily called on pages as well by using $userinfo->Twitter etc. but i have no clue about the checkboxlist… and calling the array etc.

If you guys came across any article or chunk of code which can help me get this resolved i wil be thankful 🙂
cheers
Ayaz

1 Answer
1

Justin Tadlock has a good tutorial to get you started:

http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

There are some specifics to dealing with checkboxes however, and some custom code if you want to make checkboxes that correspond to tags/categories.

To generate the form fields and save the data use the following snippet:

<?php

function user_interests_fields( $user ) {
    // get product categories
    $tags = get_terms('post_tag', array('hide_empty' => false));
    $user_tags = get_the_author_meta( 'user_interests', $user->ID );
    ?>
    <table class="form-table">
        <tr>
            <th>My interests:</th>
            <td>
        <?php
        if ( count( $tags ) ) {
            foreach( $tags as $tag ) { ?>
            <p><label for="user_interests_<?php echo esc_attr( $tag->slug); ?>">
                <input
                    id="user_interests_<?php echo esc_attr( $tag->slug); ?>"
                    name="user_interests[<?php echo esc_attr( $tag->term_id ); ?>]"
                    type="checkbox"
                    value="<?php echo esc_attr( $tag->term_id ); ?>"
                    <?php if ( in_array( $tag->term_id, $user_tags ) ) echo ' checked="checked"'; ?> />
                <?php echo esc_html($tag->name); ?>
            </label></p><?php
            }
        } ?>
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'user_interests_fields' );
add_action( 'edit_user_profile', 'user_interests_fields' );

    // store interests
    function user_interests_fields_save( $user_id ) {
        if ( !current_user_can( 'edit_user', $user_id ) )
            return false;
        update_user_meta( $user_id, 'user_interests', $_POST['user_interests'] );
    }
    add_action( 'personal_options_update', 'user_interests_fields_save' );
    add_action( 'edit_user_profile_update', 'user_interests_fields_save' );

?>

You can then call the get_the_author_meta() function to get at your array of tag IDs which can then be used in a query eg:

query_posts( array( 'tag_id' => get_the_author_meta( 'user_interests', $user_id ) ) );

Leave a Comment