get_front_page_template. $template_hierarchy_filter = str_replace( '-', '', $template_type ) . '_template_hierarchy'; // front-page -> frontpage_template_hierarchy. $result = array(); $template_hierarchy_filter_function = function( $templates ) use ( &$result ) { $result = $templates; return $templates; }; add_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20, 1 ); call_user_func( $get_template_function ); // This invokes template_hierarchy_filter. remove_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20 ); return $result; } /** * Filters into the "{$type}_template" hooks to redirect them to the Full Site Editing template canvas. * * Internally, this communicates the block content that needs to be used by the template canvas through a global variable. * * @param string $template Path to the template. See locate_template(). * @param string $type Sanitized filename without extension. * @param array $templates A list of template candidates, in descending order of priority. * @return string The path to the Full Site Editing template canvas file. */ function gutenberg_override_query_template( $template, $type, array $templates = array() ) { global $_wp_current_template_content; $current_template = gutenberg_resolve_template( $type, $templates ); if ( $current_template ) { $_wp_current_template_content = empty( $current_template->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template->post_content; if ( isset( $_GET['_wp-find-template'] ) ) { wp_send_json_success( $current_template ); } } else { if ( 'index' === $type ) { if ( isset( $_GET['_wp-find-template'] ) ) { wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) ); } } else { return false; // So that the template loader keeps looking for templates. } } // Add hooks for template canvas. // Add viewport meta tag. add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 ); // Render title tag with content, regardless of whether theme has title-tag support. remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering... add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); // ...and make it unconditional. // This file will be included instead of the theme's template file. return gutenberg_dir_path() . 'lib/template-canvas.php'; } /** * Return the correct 'wp_template' to render fot the request template type. * * Accepts an optional $template_hierarchy argument as a hint. * * @param string $template_type The current template type. * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority. * @return null|array { * @type WP_Post|null template_post A template post object, or null if none could be found. * @type int[] A list of template parts IDs for the template. * } */ function gutenberg_resolve_template( $template_type, $template_hierarchy = array() ) { if ( ! $template_type ) { return null; } if ( empty( $template_hierarchy ) ) { if ( 'index' === $template_type ) { $template_hierarchy = get_template_hierarchy( 'index' ); } else { $template_hierarchy = array_merge( get_template_hierarchy( $template_type ), get_template_hierarchy( 'index' ) ); } } $slugs = array_map( 'gutenberg_strip_php_suffix', $template_hierarchy ); // Find all potential templates 'wp_template' post matching the hierarchy. $template_query = new WP_Query( array( 'post_type' => 'wp_template', 'post_status' => array( 'publish', 'auto-draft' ), 'post_name__in' => $slugs, 'orderby' => 'post_name__in', 'posts_per_page' => -1, 'no_found_rows' => true, 'tax_query' => array( array( 'taxonomy' => 'wp_theme', 'field' => 'slug', 'terms' => wp_get_theme()->get_stylesheet(), ), ), ) ); $templates = $template_query->get_posts(); // Order these templates per slug priority. // Build map of template slugs to their priority in the current hierarchy. $slug_priorities = array_flip( $slugs ); usort( $templates, function ( $template_a, $template_b ) use ( $slug_priorities ) { return $slug_priorities[ $template_a->post_name ] - $slug_priorities[ $template_b->post_name ]; } ); return count( $templates ) ? $templates[0] : null; } /** * Displays title tag with content, regardless of whether theme has title-tag support. * * @see _wp_render_title_tag() */ function gutenberg_render_title_tag() { echo '