Definition:
function wp_update_term( $term_id, $taxonomy, $args = array() {}
Update term based on arguments provided.
The $args will indiscriminately override all values with the same field name. Care must be taken to not override important information need to update or update will fail (or perhaps create a new term, neither would be acceptable).
Parameters
- int $term_id: The ID of the term
- string $taxonomy: The context in which to relate the term to the object.
- array|string $args: Overwrite term field values
Return values
returns:Returns Term ID and Taxonomy Term ID
Defined filters
- wp_update_term_parent
apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) - term_id_filter
apply_filters('term_id_filter', $term_id, $tt_id)
Defined actions
- edit_terms
do_action( 'edit_terms', $alias->term_id ); - edited_terms
do_action( 'edited_terms', $alias->term_id ); - edit_terms
do_action( 'edit_terms', $term_id ); - edited_terms
do_action( 'edited_terms', $term_id ); - edit_term_taxonomy
do_action( 'edit_term_taxonomy', $tt_id, $taxonomy ); - edited_term_taxonomy
do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); - edit_term
do_action("edit_term", $term_id, $tt_id, $taxonomy); - edit_$taxonomy
do_action("edit_$taxonomy", $term_id, $tt_id); - edited_term
do_action("edited_term", $term_id, $tt_id, $taxonomy); - edited_$taxonomy
do_action("edited_$taxonomy", $term_id, $tt_id);
Source code
function wp_update_term( $term_id, $taxonomy, $args = array() ) {
global $wpdb;
if ( ! taxonomy_exists($taxonomy) )
return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
$term_id = (int) $term_id;
// First, get all of the original args
$term = get_term ($term_id, $taxonomy, ARRAY_A);
if ( is_wp_error( $term ) )
return $term;
// Escape data pulled from DB.
$term = add_magic_quotes($term);
// Merge old and new args with new args overwriting old ones.
$args = array_merge($term, $args);
$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
$args = wp_parse_args($args, $defaults);
$args = sanitize_term($args, $taxonomy, 'db');
extract($args, EXTR_SKIP);
// expected_slashed ($name)
$name = stripslashes($name);
$description = stripslashes($description);
if ( '' == trim($name) )
return new WP_Error('empty_term_name', __('A name is required for this term'));
$empty_slug = false;
if ( empty($slug) ) {
$empty_slug = true;
$slug = sanitize_title($name);
}
if ( $alias_of ) {
$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
if ( $alias->term_group ) {
// The alias we want is already in a group, so let's use that one.
$term_group = $alias->term_group;
} else {
// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
do_action( 'edit_terms', $alias->term_id );
$wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
do_action( 'edited_terms', $alias->term_id );
}
}
// Check $parent to see if it will cause a hierarchy loop
$parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args );
// Check for duplicate slug
$id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
if ( $id && ($id != $term_id) ) {
// If an empty slug was passed or the parent changed, reset the slug to something unique.
// Otherwise, bail.
if ( $empty_slug || ( $parent != $term['parent']) )
$slug = wp_unique_term_slug($slug, (object) $args);
else
return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
}
do_action( 'edit_terms', $term_id );
$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
if ( empty($slug) ) {
$slug = sanitize_title($name, $term_id);
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
}
do_action( 'edited_terms', $term_id );
$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) );
do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
do_action("edit_term", $term_id, $tt_id, $taxonomy);
do_action("edit_$taxonomy", $term_id, $tt_id);
$term_id = apply_filters('term_id_filter', $term_id, $tt_id);
clean_term_cache($term_id, $taxonomy);
do_action("edited_term", $term_id, $tt_id, $taxonomy);
do_action("edited_$taxonomy", $term_id, $tt_id);
return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
}
4235

February 12, 2011 


No comments yet... Be the first to leave a reply!