WP_REST_Posts_Controller::handle_template()publicWP 4.7.0

Sets the template for a post.

Method of the class: WP_REST_Posts_Controller{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_REST_Posts_Controller = new WP_REST_Posts_Controller();
$WP_REST_Posts_Controller->handle_template( $template, $post_id, $validate );
$template(string) (required)
Page template filename.
$post_id(int) (required)
Post ID.
$validate(true|false)
Whether to validate that the template selected is valid.
Default: false

Changelog

Since 4.7.0 Introduced.
Since 4.9.0 Added the $validate parameter.

WP_REST_Posts_Controller::handle_template() code WP 6.6.2

public function handle_template( $template, $post_id, $validate = false ) {

	if ( $validate && ! array_key_exists( $template, wp_get_theme()->get_page_templates( get_post( $post_id ) ) ) ) {
		$template = '';
	}

	update_post_meta( $post_id, '_wp_page_template', $template );
}