Last active
February 23, 2022 15:16
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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