Skip to content

Instantly share code, notes, and snippets.

@knuch
Created July 24, 2019 08:32
Show Gist options
  • Save knuch/ffdc55c41c33d0a22430099af03d6f10 to your computer and use it in GitHub Desktop.
Save knuch/ffdc55c41c33d0a22430099af03d6f10 to your computer and use it in GitHub Desktop.
Gutenberg override core block
<?php
// https://developer.wordpress.org/reference/hooks/render_block/
add_filter( 'render_block', 'foo_core_gallery_filter', 10, 3);
function foo_core_gallery_filter( $block_content, $block ) {
// use blockName to only affect the desired block
if( "core/calendar" !== $block['blockName'] ) {
return $block_content;
}
// $block_content contains all the data passed to the block callback function
// $block contains the serialized block markup
// Here you have all content over the block. Either wrap it in your container or totally replace the content
$output = '<h2 class="foo-gallery">';
$output .= 'GALLERY';
$output .= '</h2>';
// return the new content of the block
return $output;
}
@kmytor
Copy link

kmytor commented Jun 19, 2020

Hi,
I have this code, which is yours, hahaha
but I can't make it work, could you guide me?
`
add_filter( 'render_block', 'foo_core_gallery_filter', 10, 3);
function foo_core_gallery_filter( $block_content, $block ) {

  if( "core/gallery" !== $block['blockName'] ) {
    return $block_content;
  }

  $output = '<li class="foo-gallery">';
  $output .= 'gallery';
  $output .= '</li>';

  return $output;
}
`

and I'm printing it like this
<?php echo foo_core_gallery_filter(); //echo ($output); //print_r( $block ); ?>
and lance this error
Fatal error: Uncaught ArgumentCountError: Too few arguments to function foo_core_gallery_filter(), 0 passed in C:\laragon\www\theme-demo\wp-content\themes\my-theme\single.php on line 62 and exactly 2 expected in

You can guide me or help me to solve this since they are learning this great wordpress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment