Add a new “perfect” brand in WooCommerce programmatically

/**
 * Adds a new "perfect" brand.
 *
 * @see https://wpcodebook.com/add-new-perfect-brand-woocommerce-programmatically/
 * @see https://wordpress.org/plugins/perfect-woocommerce-brands/
 * @see https://developer.wordpress.org/reference/functions/wp_insert_term/
 *
 * @param string $brand       Brand name.
 * @param string $slug        (optional) Brand slug.
 * @param string $description (optional) Brand description.
 * @param int    $parent      (optional) Brand parent.
 *
 * @return int|false New term ID on success.
 */
function wpcodebook_add_new_brand( $brand, $slug = '', $description = '', $parent = 0 ) {
	$args      = array( 'slug' => $slug, 'description' => $description, 'parent' => $parent );
	$term_data = wp_insert_term( $brand, 'pwb-brand', $args );
	return ( is_wp_error( $term_data ) ? false : $term_data['term_id'] );
}

Leave a Comment