WP_Theme_JSON::get_settings_values_by_slug() WordPress Method

The WP_Theme_JSON::get_settings_values_by_slug() Wordpress method can be used to get the values for a specific setting from the WordPress database. This is useful if you need to access a setting's value outside of the WordPress administration area.

WP_Theme_JSON::get_settings_values_by_slug( array $settings, array $preset_metadata, array $origins ) #

Gets preset values keyed by slugs based on settings and metadata.


Description

$settings = array(
    'typography' => array(
        'fontFamilies' => array(
            array(
                'slug'       => 'sansSerif',
                'fontFamily' => '"Helvetica Neue", sans-serif',
            ),
            array(
                'slug'   => 'serif',
                'colors' => 'Georgia, serif',
            )
        ),
    ),
);
$meta = array(
   'path'      => array( 'typography', 'fontFamilies' ),
   'value_key' => 'fontFamily',
);
$values_by_slug = get_settings_values_by_slug();
// $values_by_slug === array(
//   'sans-serif' => '"Helvetica Neue", sans-serif',
//   'serif'      => 'Georgia, serif',
// );

Top ↑

Parameters

$settings

(array)(Required)Settings to process.

$preset_metadata

(array)(Required)One of the PRESETS_METADATA values.

$origins

(array)(Required)List of origins to process.


Top ↑

Return

(array) Array of presets where each key is a slug and each value is the preset value.


Top ↑

Source

File: wp-includes/class-wp-theme-json.php

	protected static function get_settings_values_by_slug( $settings, $preset_metadata, $origins ) {
		$preset_per_origin = _wp_array_get( $settings, $preset_metadata['path'], array() );

		$result = array();
		foreach ( $origins as $origin ) {
			if ( ! isset( $preset_per_origin[ $origin ] ) ) {
				continue;
			}
			foreach ( $preset_per_origin[ $origin ] as $preset ) {
				$slug = _wp_to_kebab_case( $preset['slug'] );

				$value = '';
				if ( isset( $preset_metadata['value_key'], $preset[ $preset_metadata['value_key'] ] ) ) {
					$value_key = $preset_metadata['value_key'];
					$value     = $preset[ $value_key ];
				} elseif (
					isset( $preset_metadata['value_func'] ) &&
					is_callable( $preset_metadata['value_func'] )
				) {
					$value_func = $preset_metadata['value_func'];
					$value      = call_user_func( $value_func, $preset );
				} else {
					// If we don't have a value, then don't add it to the result.
					continue;
				}

				$result[ $slug ] = $value;
			}
		}
		return $result;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.