Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93

Warning: Undefined array key "page_file_edit_line" in /home/sportsfever/public_html/filemanger/edit_text_line.php on line 32
/home/sportsfe.../httpdocs/clone/wp-inclu...
File: taxonomy.php
* @param int $term Term ID.
[2000] Fix | Delete
* @param string $taxonomy Taxonomy name.
[2001] Fix | Delete
* @param array|string $args {
[2002] Fix | Delete
* Optional. Array of arguments to override the default term ID. Default empty array.
[2003] Fix | Delete
*
[2004] Fix | Delete
* @type int $default The term ID to make the default term. This will only override
[2005] Fix | Delete
* the terms found if there is only one term found. Any other and
[2006] Fix | Delete
* the found terms are used.
[2007] Fix | Delete
* @type bool $force_default Optional. Whether to force the supplied term as default to be
[2008] Fix | Delete
* assigned even if the object was not going to be term-less.
[2009] Fix | Delete
* Default false.
[2010] Fix | Delete
* }
[2011] Fix | Delete
* @return bool|int|WP_Error True on success, false if term does not exist. Zero on attempted
[2012] Fix | Delete
* deletion of default Category. WP_Error if the taxonomy does not exist.
[2013] Fix | Delete
*/
[2014] Fix | Delete
function wp_delete_term( $term, $taxonomy, $args = array() ) {
[2015] Fix | Delete
global $wpdb;
[2016] Fix | Delete
[2017] Fix | Delete
$term = (int) $term;
[2018] Fix | Delete
[2019] Fix | Delete
$ids = term_exists( $term, $taxonomy );
[2020] Fix | Delete
if ( ! $ids ) {
[2021] Fix | Delete
return false;
[2022] Fix | Delete
}
[2023] Fix | Delete
if ( is_wp_error( $ids ) ) {
[2024] Fix | Delete
return $ids;
[2025] Fix | Delete
}
[2026] Fix | Delete
[2027] Fix | Delete
$tt_id = $ids['term_taxonomy_id'];
[2028] Fix | Delete
[2029] Fix | Delete
$defaults = array();
[2030] Fix | Delete
[2031] Fix | Delete
if ( 'category' === $taxonomy ) {
[2032] Fix | Delete
$defaults['default'] = (int) get_option( 'default_category' );
[2033] Fix | Delete
if ( $defaults['default'] === $term ) {
[2034] Fix | Delete
return 0; // Don't delete the default category.
[2035] Fix | Delete
}
[2036] Fix | Delete
}
[2037] Fix | Delete
[2038] Fix | Delete
// Don't delete the default custom taxonomy term.
[2039] Fix | Delete
$taxonomy_object = get_taxonomy( $taxonomy );
[2040] Fix | Delete
if ( ! empty( $taxonomy_object->default_term ) ) {
[2041] Fix | Delete
$defaults['default'] = (int) get_option( 'default_term_' . $taxonomy );
[2042] Fix | Delete
if ( $defaults['default'] === $term ) {
[2043] Fix | Delete
return 0;
[2044] Fix | Delete
}
[2045] Fix | Delete
}
[2046] Fix | Delete
[2047] Fix | Delete
$args = wp_parse_args( $args, $defaults );
[2048] Fix | Delete
[2049] Fix | Delete
if ( isset( $args['default'] ) ) {
[2050] Fix | Delete
$default = (int) $args['default'];
[2051] Fix | Delete
if ( ! term_exists( $default, $taxonomy ) ) {
[2052] Fix | Delete
unset( $default );
[2053] Fix | Delete
}
[2054] Fix | Delete
}
[2055] Fix | Delete
[2056] Fix | Delete
if ( isset( $args['force_default'] ) ) {
[2057] Fix | Delete
$force_default = $args['force_default'];
[2058] Fix | Delete
}
[2059] Fix | Delete
[2060] Fix | Delete
/**
[2061] Fix | Delete
* Fires when deleting a term, before any modifications are made to posts or terms.
[2062] Fix | Delete
*
[2063] Fix | Delete
* @since 4.1.0
[2064] Fix | Delete
*
[2065] Fix | Delete
* @param int $term Term ID.
[2066] Fix | Delete
* @param string $taxonomy Taxonomy name.
[2067] Fix | Delete
*/
[2068] Fix | Delete
do_action( 'pre_delete_term', $term, $taxonomy );
[2069] Fix | Delete
[2070] Fix | Delete
// Update children to point to new parent.
[2071] Fix | Delete
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
[2072] Fix | Delete
$term_obj = get_term( $term, $taxonomy );
[2073] Fix | Delete
if ( is_wp_error( $term_obj ) ) {
[2074] Fix | Delete
return $term_obj;
[2075] Fix | Delete
}
[2076] Fix | Delete
$parent = $term_obj->parent;
[2077] Fix | Delete
[2078] Fix | Delete
$edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id );
[2079] Fix | Delete
$edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' );
[2080] Fix | Delete
[2081] Fix | Delete
/**
[2082] Fix | Delete
* Fires immediately before a term to delete's children are reassigned a parent.
[2083] Fix | Delete
*
[2084] Fix | Delete
* @since 2.9.0
[2085] Fix | Delete
*
[2086] Fix | Delete
* @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
[2087] Fix | Delete
*/
[2088] Fix | Delete
do_action( 'edit_term_taxonomies', $edit_tt_ids );
[2089] Fix | Delete
[2090] Fix | Delete
$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );
[2091] Fix | Delete
[2092] Fix | Delete
// Clean the cache for all child terms.
[2093] Fix | Delete
$edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' );
[2094] Fix | Delete
clean_term_cache( $edit_term_ids, $taxonomy );
[2095] Fix | Delete
[2096] Fix | Delete
/**
[2097] Fix | Delete
* Fires immediately after a term to delete's children are reassigned a parent.
[2098] Fix | Delete
*
[2099] Fix | Delete
* @since 2.9.0
[2100] Fix | Delete
*
[2101] Fix | Delete
* @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
[2102] Fix | Delete
*/
[2103] Fix | Delete
do_action( 'edited_term_taxonomies', $edit_tt_ids );
[2104] Fix | Delete
}
[2105] Fix | Delete
[2106] Fix | Delete
// Get the term before deleting it or its term relationships so we can pass to actions below.
[2107] Fix | Delete
$deleted_term = get_term( $term, $taxonomy );
[2108] Fix | Delete
[2109] Fix | Delete
$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
[2110] Fix | Delete
[2111] Fix | Delete
foreach ( $object_ids as $object_id ) {
[2112] Fix | Delete
if ( ! isset( $default ) ) {
[2113] Fix | Delete
wp_remove_object_terms( $object_id, $term, $taxonomy );
[2114] Fix | Delete
continue;
[2115] Fix | Delete
}
[2116] Fix | Delete
[2117] Fix | Delete
$terms = wp_get_object_terms(
[2118] Fix | Delete
$object_id,
[2119] Fix | Delete
$taxonomy,
[2120] Fix | Delete
array(
[2121] Fix | Delete
'fields' => 'ids',
[2122] Fix | Delete
'orderby' => 'none',
[2123] Fix | Delete
)
[2124] Fix | Delete
);
[2125] Fix | Delete
[2126] Fix | Delete
if ( 1 === count( $terms ) && isset( $default ) ) {
[2127] Fix | Delete
$terms = array( $default );
[2128] Fix | Delete
} else {
[2129] Fix | Delete
$terms = array_diff( $terms, array( $term ) );
[2130] Fix | Delete
if ( isset( $default ) && isset( $force_default ) && $force_default ) {
[2131] Fix | Delete
$terms = array_merge( $terms, array( $default ) );
[2132] Fix | Delete
}
[2133] Fix | Delete
}
[2134] Fix | Delete
[2135] Fix | Delete
$terms = array_map( 'intval', $terms );
[2136] Fix | Delete
wp_set_object_terms( $object_id, $terms, $taxonomy );
[2137] Fix | Delete
}
[2138] Fix | Delete
[2139] Fix | Delete
// Clean the relationship caches for all object types using this term.
[2140] Fix | Delete
$tax_object = get_taxonomy( $taxonomy );
[2141] Fix | Delete
foreach ( $tax_object->object_type as $object_type ) {
[2142] Fix | Delete
clean_object_term_cache( $object_ids, $object_type );
[2143] Fix | Delete
}
[2144] Fix | Delete
[2145] Fix | Delete
$term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) );
[2146] Fix | Delete
foreach ( $term_meta_ids as $mid ) {
[2147] Fix | Delete
delete_metadata_by_mid( 'term', $mid );
[2148] Fix | Delete
}
[2149] Fix | Delete
[2150] Fix | Delete
/**
[2151] Fix | Delete
* Fires immediately before a term taxonomy ID is deleted.
[2152] Fix | Delete
*
[2153] Fix | Delete
* @since 2.9.0
[2154] Fix | Delete
*
[2155] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2156] Fix | Delete
*/
[2157] Fix | Delete
do_action( 'delete_term_taxonomy', $tt_id );
[2158] Fix | Delete
[2159] Fix | Delete
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
[2160] Fix | Delete
[2161] Fix | Delete
/**
[2162] Fix | Delete
* Fires immediately after a term taxonomy ID is deleted.
[2163] Fix | Delete
*
[2164] Fix | Delete
* @since 2.9.0
[2165] Fix | Delete
*
[2166] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2167] Fix | Delete
*/
[2168] Fix | Delete
do_action( 'deleted_term_taxonomy', $tt_id );
[2169] Fix | Delete
[2170] Fix | Delete
// Delete the term if no taxonomies use it.
[2171] Fix | Delete
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) {
[2172] Fix | Delete
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
[2173] Fix | Delete
}
[2174] Fix | Delete
[2175] Fix | Delete
clean_term_cache( $term, $taxonomy );
[2176] Fix | Delete
[2177] Fix | Delete
/**
[2178] Fix | Delete
* Fires after a term is deleted from the database and the cache is cleaned.
[2179] Fix | Delete
*
[2180] Fix | Delete
* The {@see 'delete_$taxonomy'} hook is also available for targeting a specific
[2181] Fix | Delete
* taxonomy.
[2182] Fix | Delete
*
[2183] Fix | Delete
* @since 2.5.0
[2184] Fix | Delete
* @since 4.5.0 Introduced the `$object_ids` argument.
[2185] Fix | Delete
*
[2186] Fix | Delete
* @param int $term Term ID.
[2187] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2188] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2189] Fix | Delete
* @param WP_Term $deleted_term Copy of the already-deleted term.
[2190] Fix | Delete
* @param array $object_ids List of term object IDs.
[2191] Fix | Delete
*/
[2192] Fix | Delete
do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids );
[2193] Fix | Delete
[2194] Fix | Delete
/**
[2195] Fix | Delete
* Fires after a term in a specific taxonomy is deleted.
[2196] Fix | Delete
*
[2197] Fix | Delete
* The dynamic portion of the hook name, `$taxonomy`, refers to the specific
[2198] Fix | Delete
* taxonomy the term belonged to.
[2199] Fix | Delete
*
[2200] Fix | Delete
* Possible hook names include:
[2201] Fix | Delete
*
[2202] Fix | Delete
* - `delete_category`
[2203] Fix | Delete
* - `delete_post_tag`
[2204] Fix | Delete
*
[2205] Fix | Delete
* @since 2.3.0
[2206] Fix | Delete
* @since 4.5.0 Introduced the `$object_ids` argument.
[2207] Fix | Delete
*
[2208] Fix | Delete
* @param int $term Term ID.
[2209] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2210] Fix | Delete
* @param WP_Term $deleted_term Copy of the already-deleted term.
[2211] Fix | Delete
* @param array $object_ids List of term object IDs.
[2212] Fix | Delete
*/
[2213] Fix | Delete
do_action( "delete_{$taxonomy}", $term, $tt_id, $deleted_term, $object_ids );
[2214] Fix | Delete
[2215] Fix | Delete
return true;
[2216] Fix | Delete
}
[2217] Fix | Delete
[2218] Fix | Delete
/**
[2219] Fix | Delete
* Deletes one existing category.
[2220] Fix | Delete
*
[2221] Fix | Delete
* @since 2.0.0
[2222] Fix | Delete
*
[2223] Fix | Delete
* @param int $cat_id Category term ID.
[2224] Fix | Delete
* @return bool|int|WP_Error Returns true if completes delete action; false if term doesn't exist;
[2225] Fix | Delete
* Zero on attempted deletion of default Category; WP_Error object is
[2226] Fix | Delete
* also a possibility.
[2227] Fix | Delete
*/
[2228] Fix | Delete
function wp_delete_category( $cat_id ) {
[2229] Fix | Delete
return wp_delete_term( $cat_id, 'category' );
[2230] Fix | Delete
}
[2231] Fix | Delete
[2232] Fix | Delete
/**
[2233] Fix | Delete
* Retrieves the terms associated with the given object(s), in the supplied taxonomies.
[2234] Fix | Delete
*
[2235] Fix | Delete
* @since 2.3.0
[2236] Fix | Delete
* @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
[2237] Fix | Delete
* Introduced `$parent` argument.
[2238] Fix | Delete
* @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or
[2239] Fix | Delete
* 'all_with_object_id', an array of `WP_Term` objects will be returned.
[2240] Fix | Delete
* @since 4.7.0 Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments.
[2241] Fix | Delete
* @since 6.3.0 Passing `update_term_meta_cache` argument value false by default resulting in get_terms() to not
[2242] Fix | Delete
* prime the term meta cache.
[2243] Fix | Delete
*
[2244] Fix | Delete
* @param int|int[] $object_ids The ID(s) of the object(s) to retrieve.
[2245] Fix | Delete
* @param string|string[] $taxonomies The taxonomy names to retrieve terms from.
[2246] Fix | Delete
* @param array|string $args See WP_Term_Query::__construct() for supported arguments.
[2247] Fix | Delete
* @return WP_Term[]|int[]|string[]|string|WP_Error Array of terms, a count thereof as a numeric string,
[2248] Fix | Delete
* or WP_Error if any of the taxonomies do not exist.
[2249] Fix | Delete
* See WP_Term_Query::get_terms() for more information.
[2250] Fix | Delete
*/
[2251] Fix | Delete
function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
[2252] Fix | Delete
if ( empty( $object_ids ) || empty( $taxonomies ) ) {
[2253] Fix | Delete
return array();
[2254] Fix | Delete
}
[2255] Fix | Delete
[2256] Fix | Delete
if ( ! is_array( $taxonomies ) ) {
[2257] Fix | Delete
$taxonomies = array( $taxonomies );
[2258] Fix | Delete
}
[2259] Fix | Delete
[2260] Fix | Delete
foreach ( $taxonomies as $taxonomy ) {
[2261] Fix | Delete
if ( ! taxonomy_exists( $taxonomy ) ) {
[2262] Fix | Delete
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
[2263] Fix | Delete
}
[2264] Fix | Delete
}
[2265] Fix | Delete
[2266] Fix | Delete
if ( ! is_array( $object_ids ) ) {
[2267] Fix | Delete
$object_ids = array( $object_ids );
[2268] Fix | Delete
}
[2269] Fix | Delete
$object_ids = array_map( 'intval', $object_ids );
[2270] Fix | Delete
[2271] Fix | Delete
$defaults = array(
[2272] Fix | Delete
'update_term_meta_cache' => false,
[2273] Fix | Delete
);
[2274] Fix | Delete
[2275] Fix | Delete
$args = wp_parse_args( $args, $defaults );
[2276] Fix | Delete
[2277] Fix | Delete
/**
[2278] Fix | Delete
* Filters arguments for retrieving object terms.
[2279] Fix | Delete
*
[2280] Fix | Delete
* @since 4.9.0
[2281] Fix | Delete
*
[2282] Fix | Delete
* @param array $args An array of arguments for retrieving terms for the given object(s).
[2283] Fix | Delete
* See {@see wp_get_object_terms()} for details.
[2284] Fix | Delete
* @param int[] $object_ids Array of object IDs.
[2285] Fix | Delete
* @param string[] $taxonomies Array of taxonomy names to retrieve terms from.
[2286] Fix | Delete
*/
[2287] Fix | Delete
$args = apply_filters( 'wp_get_object_terms_args', $args, $object_ids, $taxonomies );
[2288] Fix | Delete
[2289] Fix | Delete
/*
[2290] Fix | Delete
* When one or more queried taxonomies is registered with an 'args' array,
[2291] Fix | Delete
* those params override the `$args` passed to this function.
[2292] Fix | Delete
*/
[2293] Fix | Delete
$terms = array();
[2294] Fix | Delete
if ( count( $taxonomies ) > 1 ) {
[2295] Fix | Delete
foreach ( $taxonomies as $index => $taxonomy ) {
[2296] Fix | Delete
$t = get_taxonomy( $taxonomy );
[2297] Fix | Delete
if ( isset( $t->args ) && is_array( $t->args ) && array_merge( $args, $t->args ) != $args ) {
[2298] Fix | Delete
unset( $taxonomies[ $index ] );
[2299] Fix | Delete
$terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );
[2300] Fix | Delete
}
[2301] Fix | Delete
}
[2302] Fix | Delete
} else {
[2303] Fix | Delete
$t = get_taxonomy( $taxonomies[0] );
[2304] Fix | Delete
if ( isset( $t->args ) && is_array( $t->args ) ) {
[2305] Fix | Delete
$args = array_merge( $args, $t->args );
[2306] Fix | Delete
}
[2307] Fix | Delete
}
[2308] Fix | Delete
[2309] Fix | Delete
$args['taxonomy'] = $taxonomies;
[2310] Fix | Delete
$args['object_ids'] = $object_ids;
[2311] Fix | Delete
[2312] Fix | Delete
// Taxonomies registered without an 'args' param are handled here.
[2313] Fix | Delete
if ( ! empty( $taxonomies ) ) {
[2314] Fix | Delete
$terms_from_remaining_taxonomies = get_terms( $args );
[2315] Fix | Delete
[2316] Fix | Delete
// Array keys should be preserved for values of $fields that use term_id for keys.
[2317] Fix | Delete
if ( ! empty( $args['fields'] ) && str_starts_with( $args['fields'], 'id=>' ) ) {
[2318] Fix | Delete
$terms = $terms + $terms_from_remaining_taxonomies;
[2319] Fix | Delete
} else {
[2320] Fix | Delete
$terms = array_merge( $terms, $terms_from_remaining_taxonomies );
[2321] Fix | Delete
}
[2322] Fix | Delete
}
[2323] Fix | Delete
[2324] Fix | Delete
/**
[2325] Fix | Delete
* Filters the terms for a given object or objects.
[2326] Fix | Delete
*
[2327] Fix | Delete
* @since 4.2.0
[2328] Fix | Delete
*
[2329] Fix | Delete
* @param WP_Term[]|int[]|string[]|string $terms Array of terms or a count thereof as a numeric string.
[2330] Fix | Delete
* @param int[] $object_ids Array of object IDs for which terms were retrieved.
[2331] Fix | Delete
* @param string[] $taxonomies Array of taxonomy names from which terms were retrieved.
[2332] Fix | Delete
* @param array $args Array of arguments for retrieving terms for the given
[2333] Fix | Delete
* object(s). See wp_get_object_terms() for details.
[2334] Fix | Delete
*/
[2335] Fix | Delete
$terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args );
[2336] Fix | Delete
[2337] Fix | Delete
$object_ids = implode( ',', $object_ids );
[2338] Fix | Delete
$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
[2339] Fix | Delete
[2340] Fix | Delete
/**
[2341] Fix | Delete
* Filters the terms for a given object or objects.
[2342] Fix | Delete
*
[2343] Fix | Delete
* The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The
[2344] Fix | Delete
* {@see 'get_object_terms'} filter is recommended as an alternative.
[2345] Fix | Delete
*
[2346] Fix | Delete
* @since 2.8.0
[2347] Fix | Delete
*
[2348] Fix | Delete
* @param WP_Term[]|int[]|string[]|string $terms Array of terms or a count thereof as a numeric string.
[2349] Fix | Delete
* @param string $object_ids Comma separated list of object IDs for which terms were retrieved.
[2350] Fix | Delete
* @param string $taxonomies SQL fragment of taxonomy names from which terms were retrieved.
[2351] Fix | Delete
* @param array $args Array of arguments for retrieving terms for the given
[2352] Fix | Delete
* object(s). See wp_get_object_terms() for details.
[2353] Fix | Delete
*/
[2354] Fix | Delete
return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
[2355] Fix | Delete
}
[2356] Fix | Delete
[2357] Fix | Delete
/**
[2358] Fix | Delete
* Adds a new term to the database.
[2359] Fix | Delete
*
[2360] Fix | Delete
* A non-existent term is inserted in the following sequence:
[2361] Fix | Delete
* 1. The term is added to the term table, then related to the taxonomy.
[2362] Fix | Delete
* 2. If everything is correct, several actions are fired.
[2363] Fix | Delete
* 3. The 'term_id_filter' is evaluated.
[2364] Fix | Delete
* 4. The term cache is cleaned.
[2365] Fix | Delete
* 5. Several more actions are fired.
[2366] Fix | Delete
* 6. An array is returned containing the `term_id` and `term_taxonomy_id`.
[2367] Fix | Delete
*
[2368] Fix | Delete
* If the 'slug' argument is not empty, then it is checked to see if the term
[2369] Fix | Delete
* is invalid. If it is not a valid, existing term, it is added and the term_id
[2370] Fix | Delete
* is given.
[2371] Fix | Delete
*
[2372] Fix | Delete
* If the taxonomy is hierarchical, and the 'parent' argument is not empty,
[2373] Fix | Delete
* the term is inserted and the term_id will be given.
[2374] Fix | Delete
*
[2375] Fix | Delete
* Error handling:
[2376] Fix | Delete
* If `$taxonomy` does not exist or `$term` is empty,
[2377] Fix | Delete
* a WP_Error object will be returned.
[2378] Fix | Delete
*
[2379] Fix | Delete
* If the term already exists on the same hierarchical level,
[2380] Fix | Delete
* or the term slug and name are not unique, a WP_Error object will be returned.
[2381] Fix | Delete
*
[2382] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2383] Fix | Delete
*
[2384] Fix | Delete
* @since 2.3.0
[2385] Fix | Delete
*
[2386] Fix | Delete
* @param string $term The term name to add.
[2387] Fix | Delete
* @param string $taxonomy The taxonomy to which to add the term.
[2388] Fix | Delete
* @param array|string $args {
[2389] Fix | Delete
* Optional. Array or query string of arguments for inserting a term.
[2390] Fix | Delete
*
[2391] Fix | Delete
* @type string $alias_of Slug of the term to make this term an alias of.
[2392] Fix | Delete
* Default empty string. Accepts a term slug.
[2393] Fix | Delete
* @type string $description The term description. Default empty string.
[2394] Fix | Delete
* @type int $parent The id of the parent term. Default 0.
[2395] Fix | Delete
* @type string $slug The term slug to use. Default empty string.
[2396] Fix | Delete
* }
[2397] Fix | Delete
* @return array|WP_Error {
[2398] Fix | Delete
* An array of the new term data, WP_Error otherwise.
[2399] Fix | Delete
*
[2400] Fix | Delete
* @type int $term_id The new term ID.
[2401] Fix | Delete
* @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string.
[2402] Fix | Delete
* }
[2403] Fix | Delete
*/
[2404] Fix | Delete
function wp_insert_term( $term, $taxonomy, $args = array() ) {
[2405] Fix | Delete
global $wpdb;
[2406] Fix | Delete
[2407] Fix | Delete
if ( ! taxonomy_exists( $taxonomy ) ) {
[2408] Fix | Delete
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
[2409] Fix | Delete
}
[2410] Fix | Delete
[2411] Fix | Delete
/**
[2412] Fix | Delete
* Filters a term before it is sanitized and inserted into the database.
[2413] Fix | Delete
*
[2414] Fix | Delete
* @since 3.0.0
[2415] Fix | Delete
* @since 6.1.0 The `$args` parameter was added.
[2416] Fix | Delete
*
[2417] Fix | Delete
* @param string|WP_Error $term The term name to add, or a WP_Error object if there's an error.
[2418] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2419] Fix | Delete
* @param array|string $args Array or query string of arguments passed to wp_insert_term().
[2420] Fix | Delete
*/
[2421] Fix | Delete
$term = apply_filters( 'pre_insert_term', $term, $taxonomy, $args );
[2422] Fix | Delete
[2423] Fix | Delete
if ( is_wp_error( $term ) ) {
[2424] Fix | Delete
return $term;
[2425] Fix | Delete
}
[2426] Fix | Delete
[2427] Fix | Delete
if ( is_int( $term ) && 0 === $term ) {
[2428] Fix | Delete
return new WP_Error( 'invalid_term_id', __( 'Invalid term ID.' ) );
[2429] Fix | Delete
}
[2430] Fix | Delete
[2431] Fix | Delete
if ( '' === trim( $term ) ) {
[2432] Fix | Delete
return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) );
[2433] Fix | Delete
}
[2434] Fix | Delete
[2435] Fix | Delete
$defaults = array(
[2436] Fix | Delete
'alias_of' => '',
[2437] Fix | Delete
'description' => '',
[2438] Fix | Delete
'parent' => 0,
[2439] Fix | Delete
'slug' => '',
[2440] Fix | Delete
);
[2441] Fix | Delete
$args = wp_parse_args( $args, $defaults );
[2442] Fix | Delete
[2443] Fix | Delete
if ( (int) $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
[2444] Fix | Delete
return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
[2445] Fix | Delete
}
[2446] Fix | Delete
[2447] Fix | Delete
$args['name'] = $term;
[2448] Fix | Delete
$args['taxonomy'] = $taxonomy;
[2449] Fix | Delete
[2450] Fix | Delete
// Coerce null description to strings, to avoid database errors.
[2451] Fix | Delete
$args['description'] = (string) $args['description'];
[2452] Fix | Delete
[2453] Fix | Delete
$args = sanitize_term( $args, $taxonomy, 'db' );
[2454] Fix | Delete
[2455] Fix | Delete
// expected_slashed ($name)
[2456] Fix | Delete
$name = wp_unslash( $args['name'] );
[2457] Fix | Delete
$description = wp_unslash( $args['description'] );
[2458] Fix | Delete
$parent = (int) $args['parent'];
[2459] Fix | Delete
[2460] Fix | Delete
// Sanitization could clean the name to an empty string that must be checked again.
[2461] Fix | Delete
if ( '' === $name ) {
[2462] Fix | Delete
return new WP_Error( 'invalid_term_name', __( 'Invalid term name.' ) );
[2463] Fix | Delete
}
[2464] Fix | Delete
[2465] Fix | Delete
$slug_provided = ! empty( $args['slug'] );
[2466] Fix | Delete
if ( ! $slug_provided ) {
[2467] Fix | Delete
$slug = sanitize_title( $name );
[2468] Fix | Delete
} else {
[2469] Fix | Delete
$slug = $args['slug'];
[2470] Fix | Delete
}
[2471] Fix | Delete
[2472] Fix | Delete
$term_group = 0;
[2473] Fix | Delete
if ( $args['alias_of'] ) {
[2474] Fix | Delete
$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
[2475] Fix | Delete
if ( ! empty( $alias->term_group ) ) {
[2476] Fix | Delete
// The alias we want is already in a group, so let's use that one.
[2477] Fix | Delete
$term_group = $alias->term_group;
[2478] Fix | Delete
} elseif ( ! empty( $alias->term_id ) ) {
[2479] Fix | Delete
/*
[2480] Fix | Delete
* The alias is not in a group, so we create a new one
[2481] Fix | Delete
* and add the alias to it.
[2482] Fix | Delete
*/
[2483] Fix | Delete
$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
[2484] Fix | Delete
[2485] Fix | Delete
wp_update_term(
[2486] Fix | Delete
$alias->term_id,
[2487] Fix | Delete
$taxonomy,
[2488] Fix | Delete
array(
[2489] Fix | Delete
'term_group' => $term_group,
[2490] Fix | Delete
)
[2491] Fix | Delete
);
[2492] Fix | Delete
}
[2493] Fix | Delete
}
[2494] Fix | Delete
[2495] Fix | Delete
/*
[2496] Fix | Delete
* Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
[2497] Fix | Delete
* unless a unique slug has been explicitly provided.
[2498] Fix | Delete
*/
[2499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function