On this page
WP_REST_Posts_Controller::handle_template( string $template, int $post_id, bool $validate = false )
Sets the template for a post.
Parameters
$templatestring Required-
Page template filename.
$post_idint Required-
Post ID.
$validatebool Optional-
Whether to validate that the template selected is valid.
Default:
false
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php. View all references
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 );
}
Related
Uses
| Uses | Description |
|---|---|
| update_post_meta() wp-includes/post.php | Updates a post meta field based on the given post ID. |
| WP_Theme::get_page_templates() wp-includes/class-wp-theme.php | Returns the theme’s post templates for a given post type. |
| wp_get_theme() wp-includes/theme.php | Gets a WP_Theme object for a theme. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Posts_Controller::create_item() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Creates a single post. |
| WP_REST_Posts_Controller::update_item() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Updates a single post. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/handle_template