BP_XProfile_Field::set_member_types( array $member_types, bool $append = false )

Sets the profile types for this field.

Description

Parameters

$member_types

(array) (Required) Array of profile types. Can include 'null' (users with no type) in addition to any registered types.

$append

(bool) (Optional) Whether to append to existing profile types. If false, all existing profile type associations will be deleted before adding your $member_types.

Default value: false

Return

(array) profile types for the current field, after being saved.

Source

File: bp-xprofile/classes/class-bp-xprofile-field.php

	public function set_member_types( $member_types, $append = false ) {
		// Unset invalid profile types.
		$types = array();
		foreach ( $member_types as $member_type ) {
			// 'null' is a special case - it represents users without a type.
			if ( 'null' === $member_type || bp_get_member_type_object( $member_type ) ) {
				$types[] = $member_type;
			}
		}

		// When `$append` is false, delete all existing types before adding new ones.
		if ( ! $append ) {
			bp_xprofile_delete_meta( $this->id, 'field', 'member_type' );

			/*
			 * We interpret an empty array as disassociating the field from all types. This is
			 * represented internally with the '_none' flag.
			 */
			if ( empty( $types ) ) {
				bp_xprofile_add_meta( $this->id, 'field', 'member_type', '_none' );
			}
		}

		/*
		 * Unrestricted fields are represented in the database as having no 'member_type'.
		 * We detect whether a field is being set to unrestricted by checking whether the
		 * list of types passed to the method is the same as the list of registered types,
		 * plus the 'null' pseudo-type.
		 */
		$_rtypes  = bp_get_member_types();
		$rtypes   = array_values( $_rtypes );
		$rtypes[] = 'null';

		sort( $types );
		sort( $rtypes );

		// Only save if this is a restricted field.
		if ( $types !== $rtypes ) {
			// Save new types.
			foreach ( $types as $type ) {
				bp_xprofile_add_meta( $this->id, 'field', 'member_type', $type );
			}
		}

		// Reset internal cache of profile types.
		$this->member_types = null;

		/**
		 * Fires after a field's profile types have been updated.
		 *
		 * @since BuddyPress 2.4.0
		 *
		 * @param BP_XProfile_Field $this Field object.
		 */
		do_action( 'bp_xprofile_field_set_member_type', $this );

		// Refetch fresh items from the database.
		return $this->get_member_types();
	}

Changelog

Changelog
Version Description
BuddyPress 2.4.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.