Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_update_term [ WordPress Function ]

wp_update_term ( $term_id, $taxonomy, $args = array() )
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
Uses:
Returns:
  • (array|WP_Error) Returns Term ID and Taxonomy Term ID
Defined at:



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).

Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not defined in $args already.

'alias_of' will create a term group, if it doesn't already exist, and update it for the $term.

If the 'slug' argument in $args is missing, then the 'name' in $args will be used. It should also be noted that if you set 'slug' and it isn't unique then a WP_Error will be passed back. If you don't pass any slug, then a unique one will be created for you.

For what can be overrode in $args, check the term scheme can contain and stay away from the term keys.

Source


<?php
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$taxonomyARRAY_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($argsEXTR_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->termscompact('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$taxonomycompactarray_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 &#8220;%s&#8221; is already in use by another term'), $slug));
    }
    
do_action'edit_terms'$term_id );
    
$wpdb->update($wpdb->termscompact'name''slug''term_group' ), compact'term_id' ) );
    if ( empty(
$slug) ) {
        
$slug sanitize_title($name$term_id);
        
$wpdb->update$wpdb->termscompact'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_taxonomycompact'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);
}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics