W3cubDocs

/WordPress

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

Check whether the template is valid for the given post.

Description

Parameters

$template

(string) (Required) Page template filename.

$request

(WP_REST_Request) (Required) Request.

Return

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

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'] ) {
			$current_template = get_page_template_slug( $request['id'] );
		} else {
			$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( get_post( $request['id'] ), $this->post_type );

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

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

Changelog

Version Description
4.9.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/check_template