Title: WP_Font_Utils::sanitize_from_schema
Published: April 3, 2024
Last modified: May 20, 2026

---

# WP_Font_Utils::sanitize_from_schema( array $tree, array $schema ): array

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Sanitizes a tree of data using a schema.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#description)󠁿

The schema structure should mirror the data tree. Each value provided in the schema
should be a callable that will be applied to sanitize the corresponding value in
the data tree. Keys that are in the data tree, but not present in the schema, will
be removed in the sanitized data. Nested arrays are traversed recursively.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#parameters)󠁿

 `$tree`arrayrequired

The data to sanitize.

`$schema`arrayrequired

The schema used for sanitization.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#return)󠁿

 array The sanitized data.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#source)󠁿

    ```php
    public static function sanitize_from_schema( $tree, $schema ) {
    	if ( ! is_array( $tree ) || ! is_array( $schema ) ) {
    		return array();
    	}

    	foreach ( $tree as $key => $value ) {
    		// Remove keys not in the schema or with null/empty values.
    		if ( ! array_key_exists( $key, $schema ) ) {
    			unset( $tree[ $key ] );
    			continue;
    		}

    		$is_value_array  = is_array( $value );
    		$is_schema_array = is_array( $schema[ $key ] ) && ! is_callable( $schema[ $key ] );

    		if ( $is_value_array && $is_schema_array ) {
    			if ( wp_is_numeric_array( $value ) ) {
    				// If indexed, process each item in the array.
    				foreach ( $value as $item_key => $item_value ) {
    					$tree[ $key ][ $item_key ] = isset( $schema[ $key ][0] ) && is_array( $schema[ $key ][0] )
    						? self::sanitize_from_schema( $item_value, $schema[ $key ][0] )
    						: self::apply_sanitizer( $item_value, $schema[ $key ][0] );
    				}
    			} else {
    				// If it is an associative or indexed array, process as a single object.
    				$tree[ $key ] = self::sanitize_from_schema( $value, $schema[ $key ] );
    			}
    		} elseif ( ! $is_value_array && $is_schema_array ) {
    			// If the value is not an array but the schema is, remove the key.
    			unset( $tree[ $key ] );
    		} elseif ( ! $is_schema_array ) {
    			// If the schema is not an array, apply the sanitizer to the value.
    			$tree[ $key ] = self::apply_sanitizer( $value, $schema[ $key ] );
    		}

    		// Remove keys with null/empty values.
    		if ( empty( $tree[ $key ] ) ) {
    			unset( $tree[ $key ] );
    		}
    	}

    	return $tree;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/fonts/class-wp-font-utils.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/fonts/class-wp-font-utils.php#L175)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/fonts/class-wp-font-utils.php#L175-L217)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Font_Utils::apply_sanitizer()](https://developer.wordpress.org/reference/classes/wp_font_utils/apply_sanitizer/)`wp-includes/fonts/class-wp-font-utils.php` |

Applies a sanitizer function to a value.

  | 
| [WP_Font_Utils::sanitize_from_schema()](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/)`wp-includes/fonts/class-wp-font-utils.php` |

Sanitizes a tree of data using a schema.

  | 
| [wp_is_numeric_array()](https://developer.wordpress.org/reference/functions/wp_is_numeric_array/)`wp-includes/functions.php` |

Determines if the variable is a numeric-indexed array.

  |

| Used by | Description | 
| [WP_Font_Utils::sanitize_from_schema()](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/)`wp-includes/fonts/class-wp-font-utils.php` |

Sanitizes a tree of data using a schema.

  | 
| [WP_Font_Collection::sanitize_and_validate_data()](https://developer.wordpress.org/reference/classes/wp_font_collection/sanitize_and_validate_data/)`wp-includes/fonts/class-wp-font-collection.php` |

Sanitizes and validates the font collection data.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_font_utils/sanitize_from_schema/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.5.0](https://developer.wordpress.org/reference/since/6.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_font_utils%2Fsanitize_from_schema%2F)
before being able to contribute a note or feedback.