Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

It's not possible to define a block template for a template.php file at present. How do you work with page templates? WordPress

  • SOLVED

The handbook states that 'planned additions' include: 'Defined in a template.php file or pulled from a custom post type (wp_templates) that is site-specific.' So it isn't possible with Gutenberg at present, to use a block template with a template.php file.

So how do you approach page templates at present? Do you use the old editor for page templates with meta boxes/ advanced custom fields, for example? I have been disabling Gutenberg for templates.php, & using the old editor with advanced custom fields.

Answers (7)

2019-10-02

John Cotton answers:

I'm not sure why you need to disable some templates for Gutenberg - I guess you must have a moderately complex layout that requires that.

Remember, all Gutenberg really is a (very) fancy content editor, that allows non-tech users to create complicated HTML for output.

In the end though, it's all still HTML (and some CSS and JS I suppose) that's going out to the browser.

For content edited with Gutenberg, the_content() is triggering the render and generating the code that will appear in a particular spot in your template.

To me, it's seems secondary whether the content that is generated for your template to wrap around is Gutenberg or not. Providing your theme is triggering all the necessary hooks (wp_head, wp_footer etc), it should all work regardless.

The real challenge thus far with Gutenberg is the CSS element. Themes need to be fairly Gutenberg-aware (or, rather Gutenberg-CSS-aware) to work well with it.


Matthew Pollard comments:

Hi John, sorry I can't have explained myself well. How do you currently handle page templates now, please? Gutenberg isn't compatible with page templates yet.


John Cotton comments:

In what sense handle?

If I wanted a specific PHP file to handle page output, I would either use the comment style, to allow selection from the dashboard ( eg /* Template Name: Name of My Template */ ), or I'd go the page-{slug}.php route, depending on the precise circumstances.


Matthew Pollard comments:

Sorry, how do you use page templates .e.g about-us.php or contact.php now that Gutenberg is the default editor? It's not possible to assign a block template to page templates at present?


Matthew Pollard comments:

The handbook states that 'planned additions' includes 'Defined in a template.php file or pulled from a custom post type (wp_templates) that is site specific.' So it isn't possible with gutenberg at present, to use a block template with a template.php file. So how do you approach page templates? Do you use the old editor for page templates with meta boxes/ advanced custom fields, for example?

https://developer.wordpress.org/block-editor/developers/block-api/block-templates/


John Cotton comments:

I'm not sure I understand what you mean by "block template".

As I and others have posted, you can let WordPress know that you want a particular PHP file in your theme to render the page output either by naming that file page-{slug}.php or adding the page template comment and then selecting the template on the Gutenberg edit page.

Under the classic editor, when did your about-us.php page get used/called?


Matthew Pollard comments:

Hi John, block templates are new in WordPress. It's how layout blocks in the editor for custom post types. They are mentioned in the handbook here > https://developer.wordpress.org/block-editor/developers/block-api/block-templates/


Matthew Pollard comments:

Block templates. What are they? > https://www.cloudways.com/blog/create-wordpress-gutenberg-block-templates/


John Cotton comments:

Ah! I see what you mean now. Sorry, had a different template in mind.

So, like this typically:


public static function create_post_type() {
// creates $blocks
include \NS()->plugin_path( 'auctions' ) . '/inc/'.self::$post_type.'-block-template.php';

$args = array(
'labels' => self::default_post_labels(),
'show_in_menu' => \NS\Admin::MENU_SLUG,
// ...
'template' => $blocks,
'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail' ),
);
register_post_type( self::$post_type, $args );
}


John Cotton comments:

With the inc'd file just an array something like this:


$blocks = array(
[ 'core/gallery', [ 'className' => 'ns-heading-gallery' ] ],
[
'ns/section',
[ 'className' => 'section__introduction' ],
[
[
'core/columns',
[ 'className' => 'row no-gutters' ],
[
[
// etc etc


It is a pain to manage the template structure like this, not least if it gets updated (and thus breaks older pages). However, it's worked for me so far since where used has always be under my control.

I note that you can write "block updaters" to get around the breaking changes, but I've not had to do so to date.


Matthew Pollard comments:

Yes, that's what I mean. At present, it's not possible to use them for specific template.php files . This is something that is going to be added to WordPress core in the future. So when building page templates, do you switch back to the old editor?


John Cotton comments:

I've not had a reason to, so I can't say.

However, blocks are just "content with markup".

If your use case is that you want specific blocks rendered in the output without having to put them in the post, then have you looked at filtering post content prior to the_content hook running?


Matthew Pollard comments:

I have yes. I'm more concerned with what the editing experience is like for the user. As it's not possible to use gutenberg's block templates with a page template (template.php ).


John Cotton comments:

I'm not clear why the page template is a problem if you're letting users edit the content (blocks).

I can' think of good reason why you'd want to generate HTML in the PHP template using the Gutenberg blocks - why not just put whatever end HTML you want in there directly?

If you want to create an editable page for users, based on some standard Gutenberg block content, why not just give them a dashboard button to create such a page and then let them edit it?


Matthew Pollard comments:

I just want the page template to be editable for users. It's not possible to use the Gutenberg editor for page templates yet. So would you just revert back to using the classic/old editor for page templates?


John Cotton comments:

Through the WP Admin page edit interface, the user can only change page content (Gutenberg-generated or classic style), not whatever else is in the PHP file.

So I don't understand which content you want users to be able to edit that they can't now?


Matthew Pollard comments:

I've messaged you on twitter, John.

2019-10-02

Cesar Contreras answers:

you could use the old technique of creating the custom template, I hope I can serve that

https://developer.wordpress.org/themes/template-files-section/page-template-files/


Matthew Pollard comments:

Is this how you do it currently? As it's not currently possible to use block templates with specific page templates e.g. about-us.php. I use the old editor with advanced custom fields at present. In the future, when it's possible to assign a block template to a specific page template, then I will start using Gutenberg for page templates as well.

2019-10-02

Mohamed Ahmed answers:

Hello Mattp,

1- Simply open any text editor and paste the following code in it.


<?php
/* Template Name: MyPageTemplate */
?>

The above line of code tells WordPress that it is a template file called.

2- Upload the file into your theme files.
3- You will see 'MyPageTemplate ' as option to choose as a template for your pages

That's only you need
Best wishes


Matthew Pollard comments:

Hi Mohamed, I know that. You can't create a block template for a specific page template at present, so do you use the old editor with advanced custom fields?


Mohamed Ahmed comments:

Of course yes

2019-10-02

Mohammed Remeez answers:

Hello brother,
According to my opinion,
It is better to define a custom template for corresponding page.
To create a custom template:
Create a new file and add

<?php
/* Template Name : Test Template */
?>

You can assign this custom template for your new page.
It is better to add Acf fields for pages and it helps to customize and meet your requirements at very extend.
Try acf-pro, https://github.com/wp-premium/advanced-custom-fields-pro
Thanks, Hope it helps

2019-10-02

timDesain Nanang answers:

i am using https://wordpress.org/plugins/classic-editor/

Classic Editor is an official plugin maintained by the WordPress team that restores the previous (“classic”) WordPress editor and the “Edit Post” screen.
It makes it possible to use plugins that extend that screen, add old-style meta boxes, or otherwise depend on the previous editor.


timDesain Nanang comments:

you can switch the editor

2019-10-02

Rempty answers:

Hello
Why not just edit the template file of you custom page, or maybe create a shortcode to display the content you need.

2019-10-02

Echeverri answers:

you tried to use
<?php
/* Template Name: MyPageTemplate */
?>