Register Featured
(category) patterns from wordpress.org/patterns.
Source
function _load_remote_featured_patterns() {
$supports_core_patterns = get_theme_support( 'core-block-patterns' );
/** This filter is documented in wp-includes/block-patterns.php */
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
if ( ! $should_load_remote || ! $supports_core_patterns ) {
return;
}
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$featured_cat_id = 26; // This is the `Featured` category id from pattern directory.
$request->set_param( 'category', $featured_cat_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
$registry = WP_Block_Patterns_Registry::get_instance();
foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/featured';
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = sanitize_title( $normalized_pattern['title'] );
// Some patterns might be already registered as core patterns with the `core` prefix.
$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
if ( ! $is_registered ) {
register_block_pattern( $pattern_name, $normalized_pattern );
}
}
}
Hooks
- apply_filters( ‘should_load_remote_block_patterns’,
bool $should_load_remote ) Filter to disable remote block patterns.
User Contributed Notes
You must log in before being able to contribute a note or feedback.