skip to Main Content

All I want to do is override a core block’s save function to render the frontend with different html. When I was on WordPress 5.3 I was able to override and make it a dynamic block (which I prefer) with php:

register_block_type( 'core/file', array(
'render_callback' => 'custom_core_block_render_cb',));

But now I’ve updated to WordPress 5.6 there’s a WordPress notice saying "WP_Block_Type_Registry::register was called incorrectly. Block type "core/file" is already registered."

Is this notice important enough for me to not ignore it? If no, then is there a way to get around it without resorting to overriding the save() on the javascript side with blocks.registerBlockType since all it does it bring future problems and break the block in the future if I need to make updates (this really urks me). Also would rather not copy the entire block.

2

Answers


  1. You shouldn’t be trying to re-register a core block. WP core blocks have a filter to change the output: render_block

    Here’s the filter:
    apply_filters( 'render_block', string $block_content, array $block )

    Here’s the usage:

    // Two arguments
    add_filter( 'render_block', 'so66910341_core_file_output', 10, 2 );
    
    function so66910341_core_file_output( string $block_content, array $block ) {
        if ( $block['blockName'] === 'core/file' ) :
             // update the $block_content here
             $updated_content_markup = '<html stuff>';
             $updated_content_markup .= $block_content;
             $updated_content_markup .= '</closing html stuff>';
             
             return $updated_content_markup; // or whatever variable you use.
        endif;
    
        // Return the original block content if it's not a core/file block.
        return $block_content;
    }
    

    Here’s the documentation:
    https://developer.wordpress.org/reference/hooks/render_block/

    Login or Signup to reply.
  2. Better solution for WordPress 5.5 – change only render_callback

    add_filter( 'register_block_type_args', 'custom_register_block_type_args', 10, 3 );
    function custom_register_block_type_args( $args, $name ) {
        if( $name == 'core/file' ) {
            $args['render_callback'] = 'custom_core_block_render_cb';
        }
        return $args;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search