sanitize_term_field

函式
sanitize_term_field ( $field, $value, $term_id, $taxonomy, $context )
引數
  • (string) $field Term field to sanitize.
    Required:
  • (string) $value Search for this term value.
    Required:
  • (int) $term_id Term ID.
    Required:
  • (string) $taxonomy Taxonomy name.
    Required:
  • (string) $context Context in which to sanitize the term field. Accepts 'raw', 'edit', 'db', 'display', 'rss', 'attribute', or 'js'. Default 'display'.
    Required:
返回值
  • (mixed) Sanitized field.
定義位置
相關方法
sanitize_user_fieldsanitize_text_fieldsanitize_textarea_fieldsanitize_category_field_sanitize_text_fields
引入
2.3.0
棄用
-

sanitize_term_field: 這是一個WordPress的函式,對術語物件的一個特定欄位進行淨化。它用於驗證和淨化術語的一個特定欄位,如名稱或描述: 這個函式需要三個引數:術語物件、欄位的名稱和該欄位被淨化的背景。

根據上下文對術語中的欄位值進行淨化。

通過該函式傳遞術語欄位值應被假定為已為該術語欄位將要使用的任何上下文清理了該值。

如果沒有給出上下文或不支援的上下文,那麼將應用預設過濾器。

每個上下文都有足夠的過濾器來支援自定義過濾,而不需要建立你自己的過濾函式。只需建立一個函式來 鉤住你需要的過濾器。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 hook 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 hook 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 hook 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 );
/**
* Filters the taxonomy field for use in RSS.
*
* The dynamic portions of the hook 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( "{$taxonomy}_{$field}_rss", $value );
} else {
// Use display filters by default.
/**
* Filters the term field sanitized for display.
*
* The dynamic portion of the hook name, `$field`, refers to the term field name.
*
* @since 2.3.0
*
* @param mixed $value Value of the term field.
* @param int $term_id Term ID.
* @param string $taxonomy Taxonomy slug.
* @param string $context Context to retrieve the term field value.
*/
$value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context );
/**
* Filters the taxonomy field sanitized for display.
*
* 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.
* @param int $term_id Term ID.
* @param string $context Context to retrieve the taxonomy field value.
*/
$value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
}
if ( 'attribute' === $context ) {
$value = esc_attr( $value );
} elseif ( 'js' === $context ) {
$value = esc_js( $value );
}
// Restore the type for integer fields after esc_attr().
if ( in_array( $field, $int_fields, true ) ) {
$value = (int) $value;
}
return $value;
}
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 hook 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 hook 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 hook 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 ); /** * Filters the taxonomy field for use in RSS. * * The dynamic portions of the hook 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( "{$taxonomy}_{$field}_rss", $value ); } else { // Use display filters by default. /** * Filters the term field sanitized for display. * * The dynamic portion of the hook name, `$field`, refers to the term field name. * * @since 2.3.0 * * @param mixed $value Value of the term field. * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @param string $context Context to retrieve the term field value. */ $value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context ); /** * Filters the taxonomy field sanitized for display. * * 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. * @param int $term_id Term ID. * @param string $context Context to retrieve the taxonomy field value. */ $value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context ); } if ( 'attribute' === $context ) { $value = esc_attr( $value ); } elseif ( 'js' === $context ) { $value = esc_js( $value ); } // Restore the type for integer fields after esc_attr(). if ( in_array( $field, $int_fields, true ) ) { $value = (int) $value; } return $value; }
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 hook 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 hook 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 hook 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 );

		/**
		 * Filters the taxonomy field for use in RSS.
		 *
		 * The dynamic portions of the hook 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( "{$taxonomy}_{$field}_rss", $value );
	} else {
		// Use display filters by default.

		/**
		 * Filters the term field sanitized for display.
		 *
		 * The dynamic portion of the hook name, `$field`, refers to the term field name.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed  $value    Value of the term field.
		 * @param int    $term_id  Term ID.
		 * @param string $taxonomy Taxonomy slug.
		 * @param string $context  Context to retrieve the term field value.
		 */
		$value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context );

		/**
		 * Filters the taxonomy field sanitized for display.
		 *
		 * 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.
		 * @param int    $term_id Term ID.
		 * @param string $context Context to retrieve the taxonomy field value.
		 */
		$value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
	}

	if ( 'attribute' === $context ) {
		$value = esc_attr( $value );
	} elseif ( 'js' === $context ) {
		$value = esc_js( $value );
	}

	// Restore the type for integer fields after esc_attr().
	if ( in_array( $field, $int_fields, true ) ) {
		$value = (int) $value;
	}

	return $value;
}

常見問題

FAQs
檢視更多 >