bp_insert_term( string $term, string $taxonomy = '', array $args = array() )

Add a new taxonomy term to the database.


Parameters Parameters

$term

(string) (Required) The BP term name to add.

$taxonomy

(string) (Optional) The BP taxonomy to which to add the BP term.

Default value: ''

$args

(array) (Optional) Array of arguments for inserting a BP term.

  • 'description'
    (string) The term description. Default empty string.
  • 'slug'
    (string) The term slug to use. Default empty string.
  • 'metas'
    (array) The term metas to add.

Default value: array()


Top ↑

Return Return

(array|WP_Error) An array containing the term_id and term_taxonomy_id, WP_Error otherwise.


Source Source

File: bp-core/bp-core-taxonomy.php

function bp_insert_term( $term, $taxonomy = '', $args = array() ) {
	if ( ! taxonomy_exists( $taxonomy ) ) {
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.', 'buddypress' ) );
	}

	$site_id = bp_get_taxonomy_term_site_id( $taxonomy );

	$switched = false;
	if ( $site_id !== get_current_blog_id() ) {
		switch_to_blog( $site_id );
		bp_register_taxonomies();
		$switched = true;
	}

	$term_metas = array();
	if ( isset( $args['metas'] ) ) {
		$term_metas = (array) $args['metas'];
		unset( $args['metas'] );
	}

	/**
	 * Fires before a BP Term is added to the database.
	 *
	 * @since 7.0.0
	 *
	 * @param string $term     The BP term name to add.
	 * @param string $taxonomy The BP taxonomy to which to add the term.
	 * @param array  $args     Array of arguments for inserting a BP term.
	 */
	do_action( 'bp_before_insert_term', $term, $taxonomy, $args );

	$tt_id = wp_insert_term( $term, $taxonomy, $args );

	if ( is_wp_error( $tt_id ) ) {
		return $tt_id;
	}

	$term_id = reset( $tt_id );

	if ( $term_metas ) {
		bp_update_type_metadata( $term_id, $taxonomy, $term_metas );
	}

	if ( $switched ) {
		restore_current_blog();
	}

	/**
	 * Fires when taxonomy terms have been set on BuddyPress objects.
	 *
	 * @since 7.0.0
	 *
	 * @param array  $tt_ids    An array containing the `term_id` and `term_taxonomy_id`.
	 * @param string $taxonomy  Taxonomy name.
	 * @param array  $term_metas The term metadata.
	 */
	do_action( 'bp_insert_term', $tt_id, $taxonomy, $term_metas );

	return $tt_id;
}


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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