Hooks for the pattern library, the panel in the block editor where you browse and insert patterns from GenerateBlocks libraries, your own libraries and local patterns. GenerateBlocks Pro adds local pattern collections, global styles and forms to the library.
Jump to
- Libraries and access
- Local patterns (Pro)
- Pattern previews (Pro)
- Editor (JavaScript)
Libraries and access
generateblocks_pattern_libraries
GB Free — GenerateBlocks Pro uses it to add its library and your local collections.
The generateblocks_pattern_libraries filter adds to or changes the list of pattern libraries. Create each library with GenerateBlocks_Libraries::get_instance()->create().
Parameters: $libraries (array of library objects). Default: the default GenerateBlocks library plus any libraries saved on the site. A value that isn’t an array is treated as an empty list.
A library takes these keys: id, name, domain, publicKey, isEnabled, isDefault and isLocal.
add_filter( 'generateblocks_pattern_libraries', function( $libraries ) {
$libraries[] = GenerateBlocks_Libraries::get_instance()->create( [
'id' => 'my-library',
'name' => __( 'My library', 'my-plugin' ),
'domain' => 'https://patterns.example.com',
'publicKey' => 'my-public-key',
'isEnabled' => true,
'isDefault' => false,
'isLocal' => false,
] );
return $libraries;
} );
Related: generateblocks.patternLibrary.libraries Guide: Pattern Library
generateblocks_can_view_pattern_library
GB Free
The generateblocks_can_view_pattern_library filter chooses whether the current user can read libraries, categories and patterns through the pattern library REST routes.
Parameters: $can_view (bool). Default: true for users who can edit posts.
add_filter( 'generateblocks_can_view_pattern_library', function( $can_view ) {
return $can_view && current_user_can( 'edit_pages' );
} );
Related: generateblocks_editor_access_capability Guide: Pattern Library
generateblocks_default_open_pattern_library
GB Free
The generateblocks_default_open_pattern_library filter sets which library is open when the pattern library opens.
Parameters: $library_id (string). Default: gb_default_pro_library.
add_filter( 'generateblocks_default_open_pattern_library', function( $library_id ) {
return 'my-library';
} );
Related: generateblocks_default_open_pattern_category Guide: Pattern Library
generateblocks_default_open_pattern_category
GB Free
The generateblocks_default_open_pattern_category filter sets which category is selected when the pattern library opens.
Parameters: $category (string). Default: an empty string, which shows all categories.
add_filter( 'generateblocks_default_open_pattern_category', function( $category ) {
return 'hero';
} );
Related: generateblocks_default_open_pattern_library Guide: Pattern Library
generateblocks_template_viewer_parent_origins
GB Free — since GenerateBlocks 2.5.
The generateblocks_template_viewer_parent_origins filter changes the origins allowed to embed the pattern preview page. Use it when the editor is served from an origin WordPress can’t work out from its own URLs, such as a reverse proxy or a mapped domain. Entries that aren’t http or https URLs or origins are dropped.
Parameters: $origins (array of origins). Default: the origins derived from the site’s own URLs.
add_filter( 'generateblocks_template_viewer_parent_origins', function( $origins ) {
$origins[] = 'https://editor.example.com';
return $origins;
} );
Related: generateblocks_can_view_pattern_library Guide: Pattern Library
Local patterns (Pro)
generateblocks_use_default_pattern_term
GB Pro
The generateblocks_use_default_pattern_term filter chooses whether new patterns are put in a default Local collection (local-patterns) when you save them in the admin, over the REST API or with WP-CLI.
Parameters: $use_default_term (bool). Default: true.
add_filter( 'generateblocks_use_default_pattern_term', '__return_false' );
Related: generateblocks_pattern_library_count Guide: Pattern Library
generateblocks_pattern_library_count
GB Pro
The generateblocks_pattern_library_count filter sets how many local patterns the library loads in one request. A value that isn’t a number is replaced with the default, and a value below 1 becomes 1.
Parameters: $count (int). Default: 250.
add_filter( 'generateblocks_pattern_library_count', function( $count ) {
return 500;
} );
Related: generateblocks_pattern_library_max_libraries Guide: Pattern Library
generateblocks_pattern_library_max_libraries
GB Pro
The generateblocks_pattern_library_max_libraries filter sets the most pattern libraries that can be saved. It stops a runaway request from growing the stored list without limit.
Parameters: $max_libraries (int). Default: 50.
add_filter( 'generateblocks_pattern_library_max_libraries', function( $max_libraries ) {
return 100;
} );
Related: generateblocks_pattern_libraries Guide: Pattern Library
generateblocks_pattern_library_max_form_imports
GB Pro
The generateblocks_pattern_library_max_form_imports filter sets the most forms that can be imported in one request when you insert patterns that use forms. A request with more forms is rejected. A value below 1 becomes 1.
Parameters: $max_forms (int). Default: 50.
add_filter( 'generateblocks_pattern_library_max_form_imports', function( $max_forms ) {
return 100;
} );
Related: generateblocks_pattern_library_can_import_forms Guide: Forms
generateblocks_pattern_library_can_import_forms
GB Pro — since GenerateBlocks Pro 2.6.
The generateblocks_pattern_library_can_import_forms filter chooses whether the current user can import forms through the pattern library.
Parameters: $can_import (bool), $request (the WP_REST_Request, or null when it is checked as the editor loads). Default: true for users who can manage forms.
add_filter( 'generateblocks_pattern_library_can_import_forms', function( $can_import, $request ) {
return $can_import && current_user_can( 'manage_options' );
}, 10, 2 );
Related: generateblocks_form_capability Guide: Forms
Pattern previews (Pro)
generateblocks_pattern_preview_scripts
GB Pro
The generateblocks_pattern_preview_scripts filter changes the scripts loaded in the preview of a local pattern.
Parameters: $scripts (array of script URLs), $args (array with preview and post_content, the pattern’s preview data and content). Default: the scripts the pattern’s blocks need.
add_filter( 'generateblocks_pattern_preview_scripts', function( $scripts, $args ) {
$scripts[] = plugins_url( 'js/pattern-preview.js', __FILE__ );
return $scripts;
}, 10, 2 );
Related: generateblocks_pattern_preview_styles Guide: Pattern Library
generateblocks_pattern_preview_styles
GB Pro
The generateblocks_pattern_preview_styles filter changes the stylesheets loaded in the preview of a local pattern.
Parameters: $styles (array of stylesheet URLs), $args (array with preview and post_content). Default: the stylesheets the pattern’s blocks need.
add_filter( 'generateblocks_pattern_preview_styles', function( $styles, $args ) {
$styles[] = get_stylesheet_directory_uri() . '/pattern-preview.css';
return $styles;
}, 10, 2 );
Related: generateblocks_pattern_preview_scripts Guide: Pattern Library
Editor (JavaScript)
These are wp.hooks filters and actions. Add them with wp.hooks.addFilter() or wp.hooks.addAction() in a script that loads in the block editor.
generateblocks.patternLibrary.libraries
GB Free
The generateblocks.patternLibrary.libraries filter changes the list of libraries shown as buttons at the top of the pattern library.
Parameters: libraries (array of library objects, each with id, name, publicKey and isLocal). Default: the libraries from the server.
wp.hooks.addFilter(
'generateblocks.patternLibrary.libraries',
'my-plugin/libraries',
( libraries ) => libraries.filter( ( library ) => 'my-hidden-library' !== library.id )
);
Related: generateblocks_pattern_libraries Guide: Pattern Library
generateblocks.patternLibrary.addLibraryContent
GB Free — GenerateBlocks Pro replaces the content.
The generateblocks.patternLibrary.addLibraryContent filter changes the content of the Add Library window. In the free plugin it only says the feature needs GenerateBlocks Pro, which replaces it with the form that adds a library.
Parameters: content (an element), context (object with setShowAddLibrary and setLibraries, functions that close the window and update the list). Default: a message that the feature requires GenerateBlocks Pro.
const { createElement } = wp.element;
wp.hooks.addFilter(
'generateblocks.patternLibrary.addLibraryContent',
'my-plugin/add-library',
( content, { setShowAddLibrary } ) => createElement(
'p',
null,
'Add libraries from your settings page.'
)
);
Related: generateblocks_pattern_library_max_libraries Guide: Pattern Library
generateblocks.patterns.insertPatternButton
GB Free — GenerateBlocks Pro uses it to import the global styles and forms a pattern needs.
The generateblocks.patterns.insertPatternButton filter changes the button that inserts a pattern.
Parameters: button (an element, the Insert button), props (object with label, onClick, patterns, globalStyleData, className and disabled). Default: the primary insert button.
wp.hooks.addFilter(
'generateblocks.patterns.insertPatternButton',
'my-plugin/insert-pattern-button',
( button, props ) => button
);
Related: generateblocks.patterns.patternsList Guide: Pattern Library
generateblocks.patterns.patternsList
GB Free — GenerateBlocks Pro uses it to load a pattern’s global styles.
The generateblocks.patterns.patternsList action fires each time the list of patterns renders.
Parameters: context (object with activeLibrary, setGlobalStyleCSS, setGlobalStyleData, isLocal and cacheIsClearing). Default: none. It is an action.
wp.hooks.addAction(
'generateblocks.patterns.patternsList',
'my-plugin/patterns-list',
( { activeLibrary, isLocal } ) => {
// Run code when the list renders.
}
);
Related: generateblocks.patterns.insertPatternButton Guide: Pattern Library