bp_register_member_type( string $member_type, array $args = array() )

Register a member type.


Parameters Parameters

$member_type

(string) (Required) Unique string identifier for the member type.

$args

(array) (Optional) Array of arguments describing the member type.

  • 'labels'
    (array) Array of labels to use in various parts of the interface.
    • 'name'
      (string) Default name. Should typically be plural.
    • 'singular_name'
      (string) Singular name.
  • 'has_directory'
    (bool|string) Whether the member type should have its own type-specific directory. Pass true to use the $member_type string as the type's slug. Pass a string to customize the slug. Pass false to disable. Default: true.
  • 'show_in_list'
    (bool) Whether this member type should be shown in lists rendered by bp_member_type_list(). Default: false.
  • 'code'
    (bool) Whether this member type is registered using code. Default: true.
  • 'db_id'
    (int) The member type term ID. Default: 0.

Default value: array()


Top ↑

Return Return

(object|WP_Error) Member type object on success, WP_Error object on failure.


Source Source

File: bp-members/bp-members-functions.php

function bp_register_member_type( $member_type, $args = array() ) {
	$bp = buddypress();

	if ( isset( $bp->members->types[ $member_type ] ) ) {
		return new WP_Error( 'bp_member_type_exists', __( 'Member type already exists.', 'buddypress' ), $member_type );
	}

	$r = bp_parse_args( $args, array(
		'labels'        => array(),
		'has_directory' => true,
		'show_in_list'  => false,
		'code'          => true,
		'db_id'         => 0,
	), 'register_member_type' );

	$member_type = sanitize_key( $member_type );

	/**
	 * Filters the list of illegal member type names.
	 *
	 * - 'any' is a special pseudo-type, representing items unassociated with any member type.
	 * - 'null' is a special pseudo-type, representing users without any type.
	 * - '_none' is used internally to denote an item that should not apply to any member types.
	 *
	 * @since 2.4.0
	 *
	 * @param array $illegal_names Array of illegal names.
	 */
	$illegal_names = apply_filters( 'bp_member_type_illegal_names', array( 'any', 'null', '_none' ) );
	if ( in_array( $member_type, $illegal_names, true ) ) {
		return new WP_Error( 'bp_member_type_illegal_name', __( 'You may not register a member type with this name.', 'buddypress' ), $member_type );
	}

	// Store the post type name as data in the object (not just as the array key).
	$r['name'] = $member_type;

	// Make sure the relevant labels have been filled in.
	$default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] );
	$r['labels'] = array_merge( array(
		'name'          => $default_name,
		'singular_name' => $default_name,
	), $r['labels'] );

	// Directory slug.
	if ( $r['has_directory'] ) {
		// A string value is intepreted as the directory slug. Otherwise fall back on member type.
		if ( is_string( $r['has_directory'] ) ) {
			$directory_slug = $r['has_directory'];
		} else {
			$directory_slug = $member_type;
		}

		// Sanitize for use in URLs.
		$r['directory_slug'] = sanitize_title( $directory_slug );
		$r['has_directory']  = true;
	} else {
		$r['directory_slug'] = '';
		$r['has_directory']  = false;
	}

	// Show the list of member types on front-end (member header, for now).
	$r['show_in_list'] = (bool) $r['show_in_list'];

	$bp->members->types[ $member_type ] = $type = (object) $r;

	/**
	 * Fires after a member type is registered.
	 *
	 * @since 2.2.0
	 *
	 * @param string $member_type Member type identifier.
	 * @param object $type        Member type object.
	 */
	do_action( 'bp_registered_member_type', $member_type, $type );

	return $type;
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.