1

I am developing a plugin to extend BuddyBoss platform. Specifically I am sync'ing BB user info with Wordpress User meta data.

Most of my code works (country and regional organisation update fine), but sync'ing my 'gender' field is not working as expected. It seems to work for male, and female, but when Other is selected, it sets the wordpress user meta field as 'female'. I have two questions:

  1. Can you see any issues with this code: (ignore all the 5 output to console attempts - that is my second question below!)
function BB_WP_SYNC_update_wordpress_usermeta( $user_id, $posted_field_ids, $errors) {
    
    console_log( $user_id);
    var_dump($user_id);
    debug_to_console("inside sync function");
    error_log($user_id);
    ChromePhp::log("chromephp inside sync");

    if ( empty( $user_id ) ) {
        $user_id = bp_loggedin_user_id();
    }

    if ( empty( $errors ) ) {
        $country = xprofile_get_field_data(9, $user_id);
        $region = xprofile_get_field_data(29, $user_id);
        $gender = xprofile_get_field_data(5, $user_id);
        ChromePhp::log($gender);
        var_dump($gender);

        if ($gender == 'his_Male') {
            $wpgender = 'Male';
        } elseif ($gender = 'her_Female') {
            $wpgender = 'Female';
        } else {
            $wpgender = 'Other';
        }
        ChromePhp::log($wpgender);
        var_dump($wpgender);
        
        $dob = xprofile_get_field_data(4, $user_id);
        
        update_user_meta($user_id, 'ofc_country', $country);
        update_user_meta($user_id, 'ofc_regional_organisation', $region);
        update_user_meta($user_id, 'ofc_gender', $wpgender);
        update_user_meta($user_id, 'ofc_date_of_birth', $dob);

    } else {
        ChromePhp::log($errors);
    }
}
add_action( 'xprofile_updated_profile', 'BB_WP_SYNC_update_wordpress_usermeta', 1, 3 );

Question 2: I have tried various methods (as can be seen from the code above) to output the gender variables to the console. But nothing works. I have set up ChromePhp, and it works if I use it in another function in the same plugin, but I can't get it to work within this particular function. Same with creating a function to use JS to output to the console. I get nothing. var_dump also doesnt work - I assume because the user profile reloads once editing is done and submit is pressed. (I'm no expert, so this has been doing my head in). The function that ChromePHP works in is part of the template for a Buddyboss Addon. This outputs to the console fine, so I know ChromePhp is set up correctly:

    if ( ! function_exists( 'BB_WP_SYNC_get_settings_sections' ) ) {
    function BB_WP_SYNC_get_settings_sections() {
        ChromePhp::log('Hello console!');

        $settings = array(
            'BB_WP_SYNC_settings_section' => array(
                'page'  => 'addon',
                'title' => __( 'WP Sync Settings', 'buddyboss-wp-usermeta-sync' ),
            ),
        );

        return (array) apply_filters( 'BB_WP_SYNC_get_settings_sections', $settings );
    }
}
1
  • Okay - think I've just seen my error. My elseif and else statements are missing an '=' sign! Any help on the debug issue would be really appreciated. Havent solved that yet.
    – roly151
    Commented Jun 25, 2021 at 7:23

2 Answers 2

1

In this code:

if ($gender == 'his_Male') {
    $wpgender = 'Male';
} elseif ($gender = 'her_Female') {
    $wpgender = 'Female';
} else {
    $wpgender = 'Other';
}

You're not comparing $gender to 'her_Female', you're setting it to 'her_Female':

} elseif ($gender = 'her_Female') {

You should be using == or === (preferably ===, the difference is documented here).

Your second question is really a chrome-php issue, and not really on topic here. However, I would wager issue is likely that the xprofile_updated_profile hook is not run at a time that could log anything to the console while you were looking at it. You would be better off logging any debug information you need to a file. error_log() is the most straightforward method, but there are libraries out there that could also help.

1
  • Thanks Jacob. I had realised I had not put == or ===. And thankyou for the advice re chromephp. I'll give error_log() a go
    – roly151
    Commented Jun 26, 2021 at 10:27
0

I changed the wordpress meta gender data to match the BuddyBoss data (i.e. his_Male, her_Female, their_Other), and removed the if statement that converted it, and this function now works as expected. I would still love to understand why it didn't work for 'Other' as written.

function BB_WP_SYNC_update_wordpress_usermeta( $user_id, $posted_field_ids, $errors) {
    
    if ( empty( $user_id ) ) {
        $user_id = bp_loggedin_user_id();
    }

    if ( empty( $errors ) ) {
        $country = xprofile_get_field_data('Country', $user_id);
        $region = xprofile_get_field_data('Regional Association / Federation', $user_id);
        $gender = xprofile_get_field_data('Gender', $user_id);
        $dob = xprofile_get_field_data('Date of Birth', $user_id);
        
        update_user_meta($user_id, 'ofc_country', $country);
        update_user_meta($user_id, 'ofc_regional_organisation', $region);
        update_user_meta($user_id, 'ofc_gender', $gender);
        update_user_meta($user_id, 'ofc_date_of_birth', $dob);

    } else {
        ChromePhp::log($errors);
    }
}
add_action( 'xprofile_updated_profile', 'BB_WP_SYNC_update_wordpress_usermeta', 1, 3 );

And I'm still looking for a solution to be able to view php variables (my second question). Thankyou in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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