W3cubDocs

/WordPress

WP_REST_Posts_Controller::handle_template( string $template, int $post_id, bool $validate = false )

Sets the template for a post.

Parameters

$templatestringrequired
Page template filename.
$post_idintrequired
Post ID.
$validatebooloptional
Whether to validate that the template selected is valid.

Default:false

Source

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 );
}

Changelog

Version Description
4.9.0 Added the $validate parameter.
4.7.0 Introduced.

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