WP_REST_Posts_Controller::check_template() WordPress Method

The check_template method is a callback function used by the WP_REST_Posts_Controller class. This method is responsible for checking if a given template exists for a given post type. If the template exists, the method will return true. If the template does not exist, the method will return false.

WP_REST_Posts_Controller::check_template( string $template, WP_REST_Request $request ) #

Check whether the template is valid for the given post.


Parameters

$template

(string)(Required)Page template filename.

$request

(WP_REST_Request)(Required)Request.


Top ↑

Return

(bool|WP_Error) True if template is still valid or if the same as existing value, or false if template not supported.


Top ↑

Source

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

	public function check_template( $template, $request ) {

		if ( ! $template ) {
			return true;
		}

		if ( $request['id'] ) {
			$post             = get_post( $request['id'] );
			$current_template = get_page_template_slug( $request['id'] );
		} else {
			$post             = null;
			$current_template = '';
		}

		// Always allow for updating a post to the same template, even if that template is no longer supported.
		if ( $template === $current_template ) {
			return true;
		}

		// If this is a create request, get_post() will return null and wp theme will fallback to the passed post type.
		$allowed_templates = wp_get_theme()->get_page_templates( $post, $this->post_type );

		if ( isset( $allowed_templates[ $template ] ) ) {
			return true;
		}

		return new WP_Error(
			'rest_invalid_param',
			/* translators: 1: Parameter, 2: List of valid values. */
			sprintf( __( '%1$s is not one of %2$s.' ), 'template', implode( ', ', array_keys( $allowed_templates ) ) )
		);
	}


Top ↑

Changelog

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