bp_groups_register_group_type( string $group_type, array $args = array() )

Register a group type.


Parameters Parameters

$group_type

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

$args

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

  • 'has_directory'
    (string|bool) Set the slug to be used for custom group directory page. eg. example.com/groups/type/MY_SLUG. Default: false.
  • 'show_in_create_screen'
    (bool) Whether this group type is allowed to be selected on the group creation page. Default: false.
  • 'show_in_list'
    (bool|null) Whether this group type should be shown in lists rendered by bp_group_type_list(). Default: null. If $show_in_create_screen is true, this will default to true, unless this is set explicitly to false.
  • 'description'
    (string) A short descriptive summary of what the group type is. Currently shown on a group's "Manage > Settings" page when selecting group types.
  • 'create_screen_checked'
    (bool) If $show_in_create_screen is true, whether we should have our group type checkbox checked by default. Handy if you want to imply that the group type should be enforced, but decision lies with the group creator. Default: false.
  • '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.

Default value: array()


Top ↑

Return Return

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


Source Source

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

function bp_groups_register_group_type( $group_type, $args = array() ) {
	$bp = buddypress();

	if ( isset( $bp->groups->types[ $group_type ] ) ) {
		return new WP_Error( 'bp_group_type_exists', __( 'Group type already exists.', 'buddypress' ), $group_type );
	}

	$r = bp_parse_args( $args, array(
		'has_directory'         => false,
		'show_in_create_screen' => false,
		'show_in_list'          => null,
		'description'           => '',
		'create_screen_checked' => false,
		'labels'                => array(),
		'code'                  => true,
		'db_id'                 => 0,
	), 'register_group_type' );

	$group_type = sanitize_key( $group_type );

	/**
	 * Filters the list of illegal group type names.
	 *
	 * - 'any' is a special pseudo-type, representing items unassociated with any group 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 group types.
	 *
	 * @since 2.6.0
	 *
	 * @param array $illegal_names Array of illegal names.
	 */
	$illegal_names = apply_filters( 'bp_group_type_illegal_names', array( 'any', 'null', '_none' ) );
	if ( in_array( $group_type, $illegal_names, true ) ) {
		return new WP_Error( 'bp_group_type_illegal_name', __( 'You may not register a group type with this name.', 'buddypress' ), $group_type );
	}

	// Store the group type name as data in the object (not just as the array key).
	$r['name'] = $group_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 ( ! empty( $r['has_directory'] ) ) {
		// A string value is intepreted as the directory slug.
		if ( is_string( $r['has_directory'] ) ) {
			$directory_slug = $r['has_directory'];

		// Otherwise fall back on group type.
		} else {
			$directory_slug = $group_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;
	}

	// Type lists.
	if ( true === $r['show_in_create_screen'] && is_null( $r['show_in_list'] ) ) {
		$r['show_in_list'] = true;
	} else {
		$r['show_in_list'] = (bool) $r['show_in_list'];
	}

	$bp->groups->types[ $group_type ] = $type = (object) $r;

	/**
	 * Fires after a group type is registered.
	 *
	 * @since 2.6.0
	 *
	 * @param string $group_type Group type identifier.
	 * @param object $type       Group type object.
	 */
	do_action( 'bp_groups_register_group_type', $group_type, $type );

	return $type;
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduce $has_directory, $show_in_create_screen, $show_in_list, and $description, $create_screen_checked as $args parameters.
2.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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