WP_REST_Settings_Controller::get_item() WordPress Method

The WP_REST_Settings_Controller::get_item() method is used to retrieve a specific setting from the WordPress database. This is useful for retrieving settings that are not exposed through the WordPress admin interface. To use this method, you need to pass in the setting name as a parameter. The setting name is case-sensitive, so be sure to check the spelling and capitalization carefully. Once you have the setting name, the WP_REST_Settings_Controller::get_item() method will return the setting value as a string. If the setting does not exist, then the method will return false.

WP_REST_Settings_Controller::get_item( WP_REST_Request $request ) #

Retrieves the settings.


Parameters

$request

(WP_REST_Request)(Required)Full details about the request.


Top ↑

Return

(array|WP_Error) Array on success, or WP_Error object on failure.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

	public function get_item( $request ) {
		$options  = $this->get_registered_options();
		$response = array();

		foreach ( $options as $name => $args ) {
			/**
			 * Filters the value of a setting recognized by the REST API.
			 *
			 * Allow hijacking the setting value and overriding the built-in behavior by returning a
			 * non-null value.  The returned value will be presented as the setting value instead.
			 *
			 * @since 4.7.0
			 *
			 * @param mixed  $result Value to use for the requested setting. Can be a scalar
			 *                       matching the registered schema for the setting, or null to
			 *                       follow the default get_option() behavior.
			 * @param string $name   Setting name (as shown in REST API responses).
			 * @param array  $args   Arguments passed to register_setting() for this setting.
			 */
			$response[ $name ] = apply_filters( 'rest_pre_get_setting', null, $name, $args );

			if ( is_null( $response[ $name ] ) ) {
				// Default to a null value as "null" in the response means "not set".
				$response[ $name ] = get_option( $args['option_name'], $args['schema']['default'] );
			}

			/*
			 * Because get_option() is lossy, we have to
			 * cast values to the type they are registered with.
			 */
			$response[ $name ] = $this->prepare_value( $response[ $name ], $args['schema'] );
		}

		return $response;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.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.