Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Profile Fields move from BBpress to BuddyPress


  • Radzio125
    Participant

    @radzio125

    I used before only bbpress forum but I decided to add also buddypress. I have old bbpress user profile custom fields that I created.

    How to make they works on buddypress user profiles?

    I don’t want to use default buddypress fields creator because I want to display some extra stuff like icons and extra text based on selected value, and default profile fields creator doesn’t offer this.

    Here is code that I used:

    // Display on User Edit Page
    add_action("bbp_user_edit_after_contact", "edit_gender_field");
    function edit_gender_field()
    {
        // a random selection of genders
        $genders = array("Male", "Female");
        // selected gender, if any
        $user_gender = bbp_get_displayed_user_field('u_gender');
        ?>
        <label for="gender"><?php _e('Gender'); ?></label>
        <select name="gender" id="gender">
            <option value="">Select your gender</option>
            <?php foreach ($genders as $gender) : ?>
                <option <?php selected($user_gender, $gender); ?>><?php echo esc_attr($gender); ?></option>
            <?php endforeach; ?>
        </select>
        <?php
    }
    
    // Save when updating
    // Our profile
    add_action("personal_options_update", "save_gender_value");
    // Other users' profiles
    add_action("edit_user_profile_update", "save_gender_value");
    
    function save_gender_value($user_id)
    {
        // exclude profile.php/user-edit update
        if (is_admin()) return;
        // update preference
        if (isset($_POST['gender']) && $_POST['gender']) {
            return update_user_meta($user_id, "u_gender", sanitize_text_field($_POST['gender']));
        } else {
            return delete_user_meta($user_id, "u_gender");
        }
    }
    
    // Display on User Profile Page
    add_action("bbp_user_profile_gender", "display_gender_value");
    
    function display_gender_value()
    {
        // selected gender, if any
        $user_gender = bbp_get_displayed_user_field('u_gender');
        // making sure user has set their gender first
        if (!trim($user_gender)) return;
        // all good
        if ($user_gender == 'Male') {
            return printf('Some stuff to display if user is %s', esc_attr($user_gender));
        }
        if ($user_gender == 'Female') {
            return printf('Some stuff to display if user is %s', esc_attr($user_gender));
        }
    }

    I guess that I should change action personal_options_update. I found in web something like that xprofile_data_before_save but it doesn’t work.

    Also I think that I should change bbp_get_displayed_user_field. I changed to xprofile_get_field_data but it doesn’t work too.

    Maybe I’m doing something wrong. Please help me if you know how to make it work.

  • You must be logged in to reply to this topic.
Skip to toolbar