WordPress ships with a bunch of neat core Gutenberg blocks. However, there may be situations where you need to change the output of a Gutenberg block. In my use case, I needed to modify the core/image
block to add an image credit field I created using Advanced Custom Fields.
Like most things in WordPress, this is possible using an action and a few lines of code. Some solutions around on the internet make you call register_block_type
to override the registration of the core block (which is wrong, in my opinion).
The register_block_type_args
filter is called from within class-wp-block-type.php, which allows you to override the render_callback
property.
add_filter( 'register_block_type_args', 'core_image_block_type_args', 10, 3 ); function core_image_block_type_args( $args, $name ) { if ( $name == 'core/image' ) { $args['render_callback'] = 'modify_core_image'; } return $args; } function modify_core_image($attributes, $content) { // Modify core image return content or something new here return $content; }
Simply replace the name if statement value with the block you want to check for.