Skip to content

Instantly share code, notes, and snippets.

@larsloQ
Last active February 23, 2022 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsloQ/6378335259ad7da2305921d46f9b6f1d to your computer and use it in GitHub Desktop.
Save larsloQ/6378335259ad7da2305921d46f9b6f1d to your computer and use it in GitHub Desktop.
Overwrite core blocks settings via block.json in gutenberg (wordpress > 5.8, block-api v2)
<?php
function yours59_filter_metadata_registration( $settings, $metadata ) {
/*
copied all block configuration files form gutenberg plugin (wp-content/plugins/gutenberg/build/block-library/blocks/**) to theme folder
so that I'm able to overwrite it from here, and do not loose changes every time gutenberg plugin updates
*/
$is_core_block = strpos( $metadata['name'], 'core/' );
if ( $is_core_block === 0 ) {
$name = substr( $metadata['name'], 5 );
$path = get_template_directory() . '/block-config/' . $name . '/block.json';
if ( file_exists( $path ) ) {
$from_json = json_decode( file_get_contents( $path ), true );
if ( is_array( $from_json ) ) {
$settings = array_replace_recursive( $settings, $from_json );
}
}
}
return $settings;
};
if ( is_admin() ) {
add_filter( 'block_type_metadata_settings', 'yours59_filter_metadata_registration', 10, 2 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment