Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit 36e88e2

Browse files
committed
Merge branch 'feature/pattern-registration'
2 parents 3f5efed + ceb391f commit 36e88e2

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

includes/class-helpers.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,34 @@ public static function get_all_theme_names(): array {
106106

107107
return $names;
108108
}
109+
110+
/**
111+
* Creates a block pattern if it doesn't already exist.
112+
*
113+
* @param string $slug The slug for the block pattern.
114+
* @param string $name The name/title for the block pattern.
115+
* @param string $content The HTML content for the block pattern.
116+
*
117+
* @return void
118+
*/
119+
public static function make_pattern( string $slug, string $name, string $content ): void {
120+
// Check if a block pattern with this slug already exists.
121+
$existing_pattern = get_page_by_path( $slug, OBJECT, 'wp_block' );
122+
123+
// If pattern exists, exit early.
124+
if ( $existing_pattern ) {
125+
return;
126+
}
127+
128+
// Create the new block pattern.
129+
wp_insert_post(
130+
array(
131+
'post_title' => $name,
132+
'post_name' => $slug,
133+
'post_content' => $content,
134+
'post_status' => 'publish',
135+
'post_type' => 'wp_block',
136+
)
137+
);
138+
}
109139
}

theme-template/functions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
// Load and initialize blocks.
2727
require_once __DIR__ . '/blocks/all.php';
2828

29+
// Register any patterns that are referenced by slug in template files.
30+
require_once __DIR__ . '/includes/register-patterns.php';
31+
2932
// Register any scripts needed.
3033
require_once __DIR__ . '/includes/register-scripts.php';
3134

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* File for registering any patterns that are referenced by slug in template files.
4+
*
5+
* @package :THEME_LABEL
6+
* @creode-wordpress-theme-version :THEME_PLUGIN_VERSION
7+
*/
8+
9+
use Creode_Theme\Helpers;
10+
11+
/**
12+
* Example: Create a pattern by calling Helpers::make_pattern() with slug, name, and content.
13+
* Helpers::make_pattern( 'teaser', 'Teaser', '<!-- wp:acf/teaser {"name":"acf/teaser","data":[],"mode":"preview"} --><!-- /wp:acf/teaser -->' );
14+
*/

0 commit comments

Comments
 (0)