How To Override WordPress Gutenberg Core Blocks Output

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.