Get a list of all registered block patterns

Get a list of all registered Block Patterns in the WordPress Block Editor

Here is how you can get a list of all registered block patterns. It’s a handy little function I created to spit out a list an array of all the registered pattern’s names.

/**
 * Get an array of the names of all registered block patterns
 *
 * @return array $pattern_names
 */
function get_block_pattern_names_list() {
    $get_patterns  = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
    $pattern_names = array_map(
        function ( array $pattern ) {
            return $pattern['name'];
        },
        $get_patterns
    );
    return $pattern_names;
}

I use this to selectively remove all Core block patterns from the list of registered block patterns:

/**
 * Block Pattern removal
 *
 * @return void
 */
function remove_all_core_block_patterns() {


    // Remove all Core Patterns
    $registered_patterns = get_block_pattern_names_list();
    foreach ( $registered_patterns as $pattern_name ) {
        // if the name starts with 'core' remove it
        if ( substr( $pattern_name, 0, strlen( 'core' ) ) === 'core' ) {
            unregister_block_pattern( $pattern_name );
        }
    }
}

add_action( 'init', 'remove_all_core_block_patterns' );

You could alternatively return the $get_patterns variable if you need access to all the settings of each pattern or modify the array_map function to return something else for each pattern. To learn more about the WP_Block_Patterns_Registry class, check out the core code.

Get more tips like these and other helpful content on modern WordPress development in my biweekly newsletter.