• tshingleton

    (@tshingleton)


    I want to be able to load a specific custom pattern when creating a post (e.g. of a specific post type). I’ve gotten as far as the following:

    function load_post_type_patterns() {
        // Define an initial pattern for the 'Book' post type
        $post_type_object = get_post_type_object( 'book' );
        $post_type_object->template = array(
            array(
                'core/block',
                array(
                    'ref' => 2603, // The ID of my custom pattern
                ),
            ),
        );
    }
    
    add_action( 'init', 'load_post_type_patterns' );
    

    This works, but I have to manually select the pattern and select “Detach”. Otherwise, my edits on the page will update the pattern (even though I have “Sync” disabled). This is too counterintuitive for my editors.

    Anyone solve for this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • mdshak

    (@mdshak)

    You can load a specific custom pattern automatically when creating a post of a specific post type, you should use the block_editor_settings filter to set the initial pattern for the desired post type. Here’s how you can modify your code to get desire result.

    function load_post_type_patterns( $editor_settings, $post ) {
        // Define an initial pattern for the 'Book' post type
        if ( 'book' === $post->post_type ) {
            $editor_settings['__experimentalFeatures']['unfilteredTemplates']['book_template'] = array(
                array(
                    'title' => 'Custom Book Pattern',
                    'content' => array(
                        array(
                            'core/block',
                            array(
                                'ref' => 2603, // The ID of your custom pattern
                            ),
                        ),
                    ),
                ),
            );
            $editor_settings['template'] = 'book_template';
        }
    
        return $editor_settings;
    }
    
    add_filter( 'block_editor_settings_all', 'load_post_type_patterns', 10, 2 );
    

    This code adds a custom pattern named book_template for the ‘Book’ post type. When creating a new post of the ‘Book’ type, this pattern will be loaded automatically. Adjust the post type and pattern ID as needed for your setup.

    Thread Starter tshingleton

    (@tshingleton)

    Thanks @mdshak! This got me very close. A few things:
    – I had to reference ‘$post->post->post_type’ instead of ‘$post->post_type’ in the conditional, as the latter threw an undefined error — this appears to be working now
    – I’m getting the WSOD (wp_debug on, no errors printed on screen) if I’m using the full code snippet. I commented out the line of code that I determined is the culprit. Running as is of course means the code does nothing; I need to uncomment and solve for that line if no one can beat me to it!
    – Confirmed that ‘2601’ corresponds to the ID of a pattern on my site

    Thank you again for the help! Anyone interested in taking a shot at solving this would be appreciated!

    Here’s the code right now:

    /*
    * Load custom patterns per post type
    */
    
    function load_post_type_patterns( $editor_settings, $post ) {
    
        if ( 'book' === $post->post->post_type ) { // Define an initial pattern for the 'Book' post type
            $editor_settings['__experimentalFeatures']['unfilteredTemplates']['book_template'] = array(
                array(
                    'title' => 'Book Template',
                    'content' => array(
                        array(
                            'core/block',
                            array(
                                'ref' => 2601, // The ID of your custom pattern
                            ),
                        ),
                    ),
                ),
            );
            //$editor_settings['template'] = 'book_template';
        }
    
        return $editor_settings;
    }
    
    add_filter( 'block_editor_settings_all', 'load_post_type_patterns', 10, 2 );
    
    leifer

    (@leifer)

    I ran into a similar issue and found this thread looking for the answer myself. I was not able to find a way to inject (and automatically detach) a pattern. I don’t know why, but I remade several Patterns and made sure to unsync them, but none-the-less when added the way you are adding them above they behaved as though they were still synced.

    So, I didn’t find a way to add a Pattern to a Custom Post Type (and detach it).

    However, I did find a similar work around.

    Get your “lowcode” from your Pattern. Manage Patterns -> Find your Pattern -> Export as JSON.

    Take the value of the “content” of that JSON, that’s your “lowcode”.

    Example WordPress “lowcode” for a Pattern with two Paragraph blocks and two Heading blocks.

    <!-- wp:paragraph -->
    <p>My book paragraph.</p>
    <!-- /wp:paragraph -->
            
    <!-- wp:heading {"level":4} -->
    <h4 class="wp-block-heading">Book Heading</h4>
    <!-- /wp:heading -->
    
    <!-- wp:paragraph -->
    <p>Another Book paragraph</p>
    <!-- /wp:paragraph -->
            
    <!-- wp:heading {"level":4} -->
    <h4 class="wp-block-heading">Another Heading</h4>
    <!-- /wp:heading -->

    In my case, I had to do some hand-editing to remove newline characters and such, but eventually got ‘clean’ lowcode that I could then use to inject into the “content” of the post type template.

    function prefix_filter_book_content( $content, $post ) {
        if ( $post->post_type === 'book' ) {
         $content ='<!-- wp:paragraph -->
                    <p>My book paragraph.</p>
                    <!-- /wp:paragraph -->
            
                    <!-- wp:heading {"level":4} -->
                    <h4 class="wp-block-heading">Book Heading</h4>
                    <!-- /wp:heading -->
    
                    <!-- wp:paragraph -->
                    <p>Another Book paragraph</p>
                    <!-- /wp:paragraph -->
            
                   <!-- wp:heading {"level":4} -->
                   <h4 class="wp-block-heading">Another Heading</h4>
                   <!-- /wp:heading -->';
    
        }
        return $content;
    }
    add_filter( 'default_content', 'prefix_filter_book_content', 10, 2 );
    
    

    And, though the setup is a bit “hacky” the end result works. The end-user, when they create a new “Book” custom post type, they will get the “Default” Blocks added to their Editor. But since they are added as Blocks, and not as a Pattern they are treated as page / post content and saved normally without needing to detach anything.

    Thank you for listening to my TED talk.

    Thread Starter tshingleton

    (@tshingleton)

    @leifer Thanks for sharing the workaround! I may have to go this route, if I can’t find a solution here.

    Ideally, would be great to allow editors to edit patterns on the backend and having that update as appropriate, vs using the importer.

    Welp, my PM didn’t like my solution either, but his loss is your gain because I took another look at this issue ??

    Instead of getting the lowcode from my “default content” Pattern by-hand… I just do it programmatically by Post ID of my Pattern then return that. Works a mint. I feel both smart and dumb now.

    I hope WP doesn’t do anything to break this functionality, because using Patterns as “pseudo-templates” in this way is super powerful.

    function prefix_filter_book_content($content,$post) {
        if ( $post->post_type === 'book' ) {
            $my_pattern_id = 5055;
            $content_pattern = get_post($my_pattern_id);        
            $content = $content_pattern->post_content;
        }
        return $content;
    }
    add_filter( 'default_content', 'prefix_filter_book_content', 10, 2 );
    • This reply was modified 7 months, 4 weeks ago by leifer.
    Thread Starter tshingleton

    (@tshingleton)

    @leifer

    This is exactly what I was looking for! Implemented and works great!

    Thanks for taking the time to figure this one out and share

    Mike Johnston

    (@wmjohnston06)

    I just implemented @leifer’s revised code. Worked perfectly the first time. Thanks for publishing it!

    SB

    (@sairah)

    I’ve spent a lot of time trying to accomplish just exactly what @leifer’s last code snippet does flawlessly.

    One note for any others who struggled like me: the pattern post ID you have to dig into your database for. WordPress treats patterns as posts, technically, so you have to look at the timestamps in the post table and description to see when you saved the pattern. Or you can check the postmeta table also, which has less data in it so will probably be quicker. Once you have that post ID, you’re golden.

    Thanks for sharing this simple way of accomplishing what I was trying to dig into Block Bindings API as a non-coder for!

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