Title: WP_Theme::get_block_patterns
Published: November 8, 2023
Last modified: August 20, 2026

---

# WP_Theme::get_block_patterns(): array

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#description)
 * [Return](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#wp--skip-link--target)

Gets block pattern data for a specified theme.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#description)󠁿

Each pattern is defined as a PHP file and defines its metadata using plugin-style
headers. The minimum required definition is:

    ```php
    /**
     * Title: My Pattern
     * Slug: my-theme/my-pattern
     *
    ```

The output of the PHP source corresponds to the content of the pattern, e.g.:

    ```php
    <main><p><?php echo "Hello"; ?></p></main>
    ```

If applicable, this will collect from both parent and child theme.

Other settable fields include:

    ```php
    - Description
    - Viewport Width
    - Inserter         (yes/no)
    - Categories       (comma-separated values)
    - Keywords         (comma-separated values)
    - Block Types      (comma-separated values)
    - Post Types       (comma-separated values)
    - Template Types   (comma-separated values)
    ```

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#return)󠁿

 array Block pattern data.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#source)󠁿

    ```php
    public function get_block_patterns() {
    	$can_use_cached = ! wp_is_development_mode( 'theme' );

    	$pattern_data = $this->get_pattern_cache();
    	if ( is_array( $pattern_data ) ) {
    		if ( $can_use_cached ) {
    			return $pattern_data;
    		}
    		// If in development mode, clear pattern cache.
    		$this->delete_pattern_cache();
    	}

    	$dirpath      = $this->get_stylesheet_directory() . '/patterns';
    	$pattern_data = array();

    	if ( ! file_exists( $dirpath ) ) {
    		if ( $can_use_cached ) {
    			$this->set_pattern_cache( $pattern_data );
    		}
    		return $pattern_data;
    	}

    	$files = (array) self::scandir( $dirpath, 'php', -1 );

    	/**
    	 * Filters list of block pattern files for a theme.
    	 *
    	 * @since 6.8.0
    	 *
    	 * @param array  $files   Array of theme files found within `patterns` directory.
    	 * @param string $dirpath Path of theme `patterns` directory being scanned.
    	 */
    	$files = apply_filters( 'theme_block_pattern_files', $files, $dirpath );

    	$dirpath = trailingslashit( $dirpath );

    	if ( ! $files ) {
    		if ( $can_use_cached ) {
    			$this->set_pattern_cache( $pattern_data );
    		}
    		return $pattern_data;
    	}

    	$default_headers = array(
    		'title'         => 'Title',
    		'slug'          => 'Slug',
    		'description'   => 'Description',
    		'viewportWidth' => 'Viewport Width',
    		'inserter'      => 'Inserter',
    		'categories'    => 'Categories',
    		'keywords'      => 'Keywords',
    		'blockTypes'    => 'Block Types',
    		'postTypes'     => 'Post Types',
    		'templateTypes' => 'Template Types',
    	);

    	$properties_to_parse = array(
    		'categories',
    		'keywords',
    		'blockTypes',
    		'postTypes',
    		'templateTypes',
    	);

    	foreach ( $files as $file ) {
    		$pattern = get_file_data( $file, $default_headers );

    		if ( empty( $pattern['slug'] ) ) {
    			_doing_it_wrong(
    				__FUNCTION__,
    				sprintf(
    					/* translators: 1: file name. */
    					__( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
    					$file
    				),
    				'6.0.0'
    			);
    			continue;
    		}

    		if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern['slug'] ) ) {
    			_doing_it_wrong(
    				__FUNCTION__,
    				sprintf(
    					/* translators: 1: file name; 2: slug value found. */
    					__( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ),
    					$file,
    					$pattern['slug']
    				),
    				'6.0.0'
    			);
    		}

    		// Title is a required property.
    		if ( ! $pattern['title'] ) {
    			_doing_it_wrong(
    				__FUNCTION__,
    				sprintf(
    					/* translators: 1: file name. */
    					__( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
    					$file
    				),
    				'6.0.0'
    			);
    			continue;
    		}

    		// For properties of type array, parse data as comma-separated.
    		foreach ( $properties_to_parse as $property ) {
    			if ( ! empty( $pattern[ $property ] ) ) {
    				$pattern[ $property ] = array_filter( wp_parse_list( (string) $pattern[ $property ] ) );
    			} else {
    				unset( $pattern[ $property ] );
    			}
    		}

    		// Parse properties of type int.
    		$property = 'viewportWidth';
    		if ( ! empty( $pattern[ $property ] ) ) {
    			$pattern[ $property ] = (int) $pattern[ $property ];
    		} else {
    			unset( $pattern[ $property ] );
    		}

    		// Parse properties of type bool.
    		$property = 'inserter';
    		if ( ! empty( $pattern[ $property ] ) ) {
    			$pattern[ $property ] = in_array(
    				strtolower( $pattern[ $property ] ),
    				array( 'yes', 'true' ),
    				true
    			);
    		} else {
    			unset( $pattern[ $property ] );
    		}

    		$key = str_replace( $dirpath, '', $file );

    		$pattern_data[ $key ] = $pattern;
    	}

    	if ( $can_use_cached ) {
    		$this->set_pattern_cache( $pattern_data );
    	}

    	return $pattern_data;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-theme.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-theme.php#L1856)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-theme.php#L1856-L2002)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#hooks)󠁿

 [apply_filters( ‘theme_block_pattern_files’, array $files, string $dirpath )](https://developer.wordpress.org/reference/hooks/theme_block_pattern_files/)

Filters list of block pattern files for a theme.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Theme::delete_pattern_cache()](https://developer.wordpress.org/reference/classes/wp_theme/delete_pattern_cache/)`wp-includes/class-wp-theme.php` |

Clears block pattern cache.

  | 
| [WP_Theme::get_pattern_cache()](https://developer.wordpress.org/reference/classes/wp_theme/get_pattern_cache/)`wp-includes/class-wp-theme.php` |

Gets block pattern cache.

  | 
| [WP_Theme::set_pattern_cache()](https://developer.wordpress.org/reference/classes/wp_theme/set_pattern_cache/)`wp-includes/class-wp-theme.php` |

Sets block pattern cache.

  | 
| [wp_is_development_mode()](https://developer.wordpress.org/reference/functions/wp_is_development_mode/)`wp-includes/load.php` |

Checks whether the site is in the given development mode.

  | 
| [wp_parse_list()](https://developer.wordpress.org/reference/functions/wp_parse_list/)`wp-includes/functions.php` |

Converts a comma- or space-separated list of scalar values to an array.

  | 
| [WP_Theme::get_stylesheet_directory()](https://developer.wordpress.org/reference/classes/wp_theme/get_stylesheet_directory/)`wp-includes/class-wp-theme.php` |

Returns the absolute path to the directory of a theme’s “stylesheet” files.

  | 
| [WP_Theme::scandir()](https://developer.wordpress.org/reference/classes/wp_theme/scandir/)`wp-includes/class-wp-theme.php` |

Scans a directory for files of a certain extension.

  | 
| [get_file_data()](https://developer.wordpress.org/reference/functions/get_file_data/)`wp-includes/functions.php` |

Retrieves metadata from a file.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [trailingslashit()](https://developer.wordpress.org/reference/functions/trailingslashit/)`wp-includes/formatting.php` |

Appends a trailing slash.

  | 
| [_doing_it_wrong()](https://developer.wordpress.org/reference/functions/_doing_it_wrong/)`wp-includes/functions.php` |

Marks something as being incorrectly called.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 7 more](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.4.0](https://developer.wordpress.org/reference/since/6.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_theme%2Fget_block_patterns%2F)
before being able to contribute a note or feedback.