BuddyDev

Search

Bulk reset profile visibility fur users bp_xprofile_visibility_levels

  • Participant
    Level: Initiated
    Posts: 3
    Dorian Logan on #34334

    We have some profiles where users have made elements of them a bit too public. I would like to reset them – back to the site default values.

    I have altered the profile defaults – all new users are set up correctly – I have also deleted the old references to bp_xprofile_visibility_levels from the DB

    However – this is not making any visible change to the user profile on the site – unless I edit the user via the front end.

    Is there a method DB / Script / Code I can use to force all users to update – I tried adding a x_profile field to all users and editing value – but it did not have any effect.

    Or is there a cache somewhere I should purge in the DB – really appreciate any pointers.

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #34338

    Hi Dorian,
    Welcome to BuddyDev forums.

    The User Profile fields data visibility is stored in user meta as an associative array of field ids as key and visibility levels as value

    Example
    array( 1=> ‘public’, 2=>’private’ and so on) where 1, 2 etc are field ids.

    Deleting the old value will make all the fields public(except if you have an enforced visibility option set foe the field).

    If your goal is not to enforce a default visibility all the time, just doing it when this was not set by the user(I am assuming you deleted the meta key), we can use two approaches:-

    1. Bulk update usermeta for each user via phpmyadmin and set the value for ‘bp_xprofile_visibility_levels’ as the serialized value of our expected setting

    2. Or the easier route is to filter on the value of the user meta ‘bp_xprofile_visibility_levels’ and if it is empty(you deleted all, if the user updated it, It won’t be empty), we return the default fields visibility.

    Which route will you like to use? Please let me know and I will assist.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 3
    Dorian Logan on #34347

    Option 2 looks good – fyi we are running buddyboss stack.

    Can you help with a filter so only first, last, company name are public. All other items are hidden.

    With this filter also fix rest API queries?

    Really appreciate your assistance

    D

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #34352

    Hi Dorian,
    Thank you for the reply.

    Yes, It should work fine for the REST API.

    Please put this in wp-content/plugins/bp-custom.php

    
    
    /**
     * Ensures default profile fields visibility is used while displaying profile
     *
     * It is only applied if user has not saved any custom visibility.
     */
    function budydev_ensure_default_xprofile_field_data_visibility_for_user( $value, $object_id, $meta_key ) {
    
    	if ( 'bp_xprofile_visibility_levels' !== $meta_key ) {
    		return $value;
    	}
    
    	// let us use a  static cache since the default visibilites won't change during a request.
    	static $default_visibilities = null;
    
    	if ( ! is_null( $default_visibilities ) ) {
    		return $default_visibilities;
    	}
    
    	$visibilities = BP_XProfile_Group::fetch_default_visibility_levels();
    
    	foreach ( $visibilities as $field_id => $visibility ) {
    		$default_visibilities[ $field_id ] = isset( $visibility['default'] ) ? $visibility['default'] : 'public';
    	}
    
    	return $default_visibilities;
    
    }
    
    add_filter( 'default_user_metadata', 'budydev_ensure_default_xprofile_field_data_visibility_for_user', 10, 3 );
    
    

    It will ensure that whatever you set as the default visibility for the profile field will be used for users who haven’t updated their profile data.

    Please let me know if it works for you or not?

    Regards
    Brajesh

    PS:- Please do not copy code from email notification. It is entity encoded. Use the code from this post on forum.

You must be logged in to reply to this topic.

This topic is: not resolved