• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / sanitize_term_field() – Cleanse the field value in the term based on the context.

You appear to be a bot. Output may be restricted

Description

Cleanse the field value in the term based on the context.

Passing a term field value through the function should be assumed to have cleansed the value for whatever context the term field is going to be used. If no context or an unsupported context is given, then default filters will be applied. There are enough filters for each context to support a custom filtering without creating your own filter function. Simply create a function that hooks into the filter you need.

Usage

$mixed = sanitize_term_field( $field, $value, $term_id, $taxonomy, $context );

Parameters

$field
( string ) required – Term field to sanitize.
$value
( string ) required – Search for this term value.
$term_id
( int ) required – Term ID.
$taxonomy
( string ) required – Taxonomy Name.
$context
( string ) required – Context in which to sanitize the term field. Accepts 'edit', 'db', 'display', 'attribute', or 'js'.

Returns

mixed Sanitized field.

Source

File name: wordpress/wp-includes/taxonomy.php
Lines:

1 to 100 of 151
function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) {
  $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
  if ( in_array( $field, $int_fields, true ) ) {
    $value = (int) $value;
    if ( $value < 0 ) {
      $value = 0;
    }
  }

  $context = strtolower( $context );

  if ( 'raw' === $context ) {
    return $value;
  }

  if ( 'edit' === $context ) {

    
/**
 * Filters a term field to edit before it is sanitized.
 *
 * The dynamic portion of the filter name, `$field`, refers to the term field.
 *
 * @since 2.3.0
 *
 * @param mixed $value     Value of the term field.
 * @param int   $term_id   Term ID.
 * @param string $taxonomy Taxonomy slug.
 */
    $value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy );

    
/**
 * Filters the taxonomy field to edit before it is sanitized.
 *
 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
 * to the taxonomy slug and taxonomy field, respectively.
 *
 * @since 2.3.0
 *
 * @param mixed $value   Value of the taxonomy field to edit.
 * @param int   $term_id Term ID.
 */
    $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );

    if ( 'description' === $field ) {
      $value = esc_html( $value ); // textarea_escaped
    } else {
      $value = esc_attr( $value );
    }
  } elseif ( 'db' === $context ) {

    
/**
 * Filters a term field value before it is sanitized.
 *
 * The dynamic portion of the filter name, `$field`, refers to the term field.
 *
 * @since 2.3.0
 *
 * @param mixed  $value    Value of the term field.
 * @param string $taxonomy Taxonomy slug.
 */
    $value = apply_filters( "pre_term_{$field}", $value, $taxonomy );

    
/**
 * Filters a taxonomy field before it is sanitized.
 *
 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
 * to the taxonomy slug and field name, respectively.
 *
 * @since 2.3.0
 *
 * @param mixed $value Value of the taxonomy field.
 */
    $value = apply_filters( "pre_{$taxonomy}_{$field}", $value );

    // Back compat filters.
    if ( 'slug' === $field ) {
      
/**
 * Filters the category nicename before it is sanitized.
 *
 * Use the {@see 'pre_$taxonomy_$field'} hook instead.
 *
 * @since 2.0.3
 *
 * @param string $value The category nicename.
 */
      $value = apply_filters( 'pre_category_nicename', $value );
    }
  } elseif ( 'rss' === $context ) {

    
/**
 * Filters the term field for use in RSS.
 *
 * The dynamic portion of the filter name, `$field`, refers to the term field.
 *
 * @since 2.3.0
 *
 * @param mixed  $value    Value of the term field.
 * @param string $taxonomy Taxonomy slug.
 */
    $value = apply_filters( "term_{$field}_rss", $value, $taxonomy );
 
[1] [2] Next »

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: sanitize_term_field
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/taxonomy.php
File ref: wp-includes/taxonomy.php
Deprecated?: No
API Letters: F,S,T

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites