wp_insert_term example



                        $term = get_term_by( 'name', $term_name$taxonomy );

                        if ( ! $term ) {
                            // Term doesn't exist, so check that the user is allowed to create new terms.                             if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->edit_terms ) ) {
                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
                            }

                            // Create the new term.                             $term_info = wp_insert_term( $term_name$taxonomy );
                            if ( is_wp_error( $term_info ) ) {
                                return new IXR_Error( 500, $term_info->get_error_message() );
                            }

                            $terms[ $taxonomy ][] = (int) $term_info['term_id'];
                        } else {
                            $terms[ $taxonomy ][] = (int) $term->term_id;
                        }
                    }
                }
            }

            
if ( empty( $parent )
        || ! term_exists( $parent$catarr['taxonomy'] )
        || ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID']$parent$catarr['taxonomy'] ) ) ) {
        $parent = 0;
    }

    $args = compact( 'name', 'slug', 'parent', 'description' );

    if ( $update ) {
        $catarr['cat_ID'] = wp_update_term( $catarr['cat_ID']$catarr['taxonomy']$args );
    } else {
        $catarr['cat_ID'] = wp_insert_term( $catarr['cat_name']$catarr['taxonomy']$args );
    }

    if ( is_wp_error( $catarr['cat_ID'] ) ) {
        if ( $wp_error ) {
            return $catarr['cat_ID'];
        } else {
            return 0;
        }
    }
    return $catarr['cat_ID']['term_id'];
}

case 'add-tag':
        check_admin_referer( 'add-tag', '_wpnonce_add-tag' );

        if ( ! current_user_can( $tax->cap->edit_terms ) ) {
            wp_die(
                '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
                '<p>' . __( 'Sorry, you are not allowed to create terms in this taxonomy.' ) . '</p>',
                403
            );
        }

        $ret = wp_insert_term( $_POST['tag-name']$taxonomy$_POST );
        if ( $ret && ! is_wp_error( $ret ) ) {
            $location = add_query_arg( 'message', 1, $referer );
        } else {
            $location = add_query_arg(
                array(
                    'error'   => true,
                    'message' => 4,
                ),
                $referer
            );
        }

        
if ( $menu_exists ) {
            return new WP_Error(
                'menu_exists',
                sprintf(
                    /* translators: %s: Menu name. */
                    __( 'The menu name %s conflicts with another menu name. Please try another.' ),
                    '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
                )
            );
        }

        $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );

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

        /** * Fires after a navigation menu is successfully created. * * @since 3.0.0 * * @param int $term_id ID of the new menu. * @param array $menu_data An array of menu data. */
$wp_taxonomies[ $taxonomy ] = $taxonomy_object;

    $taxonomy_object->add_hooks();

    // Add default term.     if ( ! empty( $taxonomy_object->default_term ) ) {
        $term = term_exists( $taxonomy_object->default_term['name']$taxonomy );
        if ( $term ) {
            update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
        } else {
            $term = wp_insert_term(
                $taxonomy_object->default_term['name'],
                $taxonomy,
                array(
                    'slug'        => sanitize_title( $taxonomy_object->default_term['slug'] ),
                    'description' => $taxonomy_object->default_term['description'],
                )
            );

            // Update `term_id` in options.             if ( ! is_wp_error( $term ) ) {
                update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
            }
$checked_categories = array_map( 'absint', (array) $post_category );
    $popular_ids        = wp_popular_terms_checklist( $taxonomy->name, 0, 10, false );

    foreach ( $names as $cat_name ) {
        $cat_name          = trim( $cat_name );
        $category_nicename = sanitize_title( $cat_name );

        if ( '' === $category_nicename ) {
            continue;
        }

        $cat_id = wp_insert_term( $cat_name$taxonomy->name, array( 'parent' => $parent ) );

        if ( ! $cat_id || is_wp_error( $cat_id ) ) {
            continue;
        } else {
            $cat_id = $cat_id['term_id'];
        }

        $checked_categories[] = $cat_id;

        if ( $parent ) { // Do these all at once in a second.             continue;
        }
if ( ! $parent ) {
                return new WP_Error(
                    'rest_term_invalid',
                    __( 'Parent term does not exist.' ),
                    array( 'status' => 400 )
                );
            }
        }

        $prepared_term = $this->prepare_item_for_database( $request );

        $term = wp_insert_term( wp_slash( $prepared_term->name )$this->taxonomy, wp_slash( (array) $prepared_term ) );
        if ( is_wp_error( $term ) ) {
            /* * If we're going to inform the client that the term already exists, * give them the identifier for future use. */
            $term_id = $term->get_error_data( 'term_exists' );
            if ( $term_id ) {
                $existing_term = get_term( $term_id$this->taxonomy );
                $term->add_data( $existing_term->term_id, 'term_exists' );
                $term->add_data(
                    array(
                        
Home | Imprint | This part of the site doesn't use cookies.