WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/sanitize meta

Description

Applies filters that can be hooked to perform specific sanitization procedures for the particular metadata type and key. Does not sanitize anything on its own. Custom filters must be hooked in to do the work. The filter hook tag has the form "sanitize_{$meta_type}_meta_{$meta_key}".

Usage

 <?php $clean_value sanitize_meta$meta_key$meta_value$meta_type ); ?> 

Parameters

$meta_key
(string) (required) The metadata key.
Default: None
$meta_value
(mixed) (required) The metadata value to be sanitized.
Default: None
$meta_type
(string) (required) The metadata type.
Default: None

Return Value

(mixed) Sanitized value as processed by any hooked filter functions.

Example

$clean_value = sanitize_meta( 'birth-year', $user_input, 'user' );

function sanitize_birth_year_meta( $year ) {

	$now = date( 'Y' );
	$then = $now - 115; // No users older than 115.

	if ( $then > $year || $year > $now ) {

		wp_die( 'Invalid entry, go back and try again.' );
	}

	return $year;
}
add_filter( 'sanitize_user_meta_birth-year', 'sanitize_birth_year_meta' );

Notes

This function is called by add_meta_data() and update_metadata() WordPress functions.

Change Log

Since: 3.1.3

Source File

sanitize_meta() is located in wp-includes/meta.php.

Related

sanitize_meta() is in a class of functions that help you sanitize potentially unsafe data which allow you to pass an arbitrary variable and receive the clean version based on data type. Others include:

See also index of Function Reference and index of Template Tags.