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

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #53259
    William
    Participant

    Hi Zac,

    The following code adds a cover image to all pages:

    function block_templates( $args, $post_type ) {
    
    	if ( $post_type === 'page' ) {
    
    		$args[ 'template' ] = [
    
    			[
    				'core/cover-image', [
    					'align' => 'full',
    				]
    			]
    			
    		];
    
    	}
    	return $args;
    
    }
    add_filter( 'register_post_type_args', 'block_templates', 20, 2 );

    I created a page template called “Homepage” located in the directory “templates/template-home.php”.

    How would I go about adding Block Templates only to the “Homepage” Page Template?

    I tried replacing ( $post_type === 'page' ) with is_page_template( 'templates/template-home.php' ) But with no result!

    Look forward to your input – Thank you 🙂

    #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!

    #53278
    Zac Gordon
    Keymaster

    Oh, my mistake he is using graphql here, but it can be done with just the WP data store I believe.

    #53328
    William
    Participant

    Hi Zac,

    Thanks for your input – but it’s a little over my head my indeed 🙂

    Looks like I have a lot JS studying to do!

    #55872
    GPorter43
    Participant

    I have tried similar with some php, in functions.php

    
    add_action('wp_enqueue_scripts','Load_BlockTemplate');
    function Load_BlockTemplate(){
        if ( is_page_template('custom-page.php') ) {
            wp_enqueue_script('BlockTemplate-script', 'path/to/script.js');
        } 
    }
    

    The problem i found was that when you create the new page its default until you publish and then when you edit the page all templates are present.
    Would love to know how to do this in js so it is dynamic and more user friendly

    #55891
    Zac Gordon
    Keymaster

    I am not exactly sure what you are trying to do here but Block Templates are covered in depth in the Gutenberg Theme Development Course. Hopefully that will help!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.