How To force WooCommerce field to be private

In this article, you will learn: How to force certain woocommerce fields to be private in the BuddyPress profile. We are going to use a hook that is available since version 3.3.11 of WC4BP.

What you have to generally do is the following:

  1. Find the IDs of the fields you want to force visibility.
  2. Update the code below with the IDs of the fields from the previous step
  3. Synchronize the user fields again, so that the new code is executed.

Step 1

To get the IDs of the fields in which you want to force visibility you can go to the Status view of WC4BP and Scroll to the WC4BP XProfile Details section as shown in the following image.

Step 2

This code forces the visibility of the fields according to their ID. The following example forces visibility to Only me for fields 522, 523, and 524. You must replace 522, 523, and 524 with the IDs you obtained in the previous step.

add_filter( 'wc4bp_xprofile_visibility', 'wc4bp_232312_force_visibility', 10, 2 );

function wc4bp_232312_force_visibility( $visibility_level, $field_id ) {
	$target_fields_ids = array( 522, 524, 523 );

	if ( in_array( $field_id, $target_fields_ids ) ) {
		return 'adminsonly'; // The possible options are public => Everyone, adminsonly => Only me, loggedin => All Members
	}

	return $visibility_level;
}

You can add that code on your site with the snippet plugin or from the function.php file of your template.

https://wordpress.org/plugins/code-snippets/

Step 3

Now you need to go to WC4BP and run the sync.

With this everything must be ready. Now you only have to verify that everything is correct. I recommend that you visit your extended profile from the backend and visit another user's profile on the fronted one.

Still need help? Contact Us Contact Us