JavaScript for WordPress Forums Gutenberg Development Adding Block Templates to a Page Template? Reply To: Adding Block Templates to a Page Template?

#53275
Zac Gordon
Keymaster

Okay this is possible, but not in this way.

We can set other conditionals in the PHP here based on anything we are aware of when the data leaves the server. So, if a certain user role then lock the template. That’s possible.

But, the way to check what template is selected and make changes based off of that information is from the client side with JavaScript.

Gutenberg has a data system (Redux store behind the scenes) that we can hook into to find out the current template and do something when it changes. The JavaScript is pretty advanced and I don’t have my Advanced JavaScript Course yet that will get into explaining it.

Jason Bhal, creator of WP GraphQL shared a ghist that shows how you can hook into this.


View this gist on GitHub

Not sure if this is over your head for implementing? You can ignore the graphql bits.

In the big picture, the ability to control templates from the PHP is going to be limited to things you can detect at the time that hook is being called, which doesn’t yet include page template (I don’t think) and certainly wouldn’t allow for changes if someone changed the template in the editor.

Hope this explains a bit!