Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tdmrhn/2b23536848fa47d4b4774264cd9bfe22 to your computer and use it in GitHub Desktop.
Save tdmrhn/2b23536848fa47d4b4774264cd9bfe22 to your computer and use it in GitHub Desktop.
Blocksy 2 Add Custom Hook to Single Posts Before Last 3rd Block
<?php
add_filter( 'the_content', function ( $content ) {
if ( is_single() ) {
$blocks = parse_blocks( $content );
$block_count = count( $blocks );
$insert_before_block = 3;
$custom_hook_content = apply_filters( 'blocksy:before:certain:last:block', '' );
if ( $block_count >= $insert_before_block ) {
array_splice( $blocks, $block_count - $insert_before_block, 0, $custom_hook_content );
} else {
$blocks[] = $custom_hook_content;
}
$content = '';
foreach ( $blocks as $block ) {
$content .= render_block( $block );
}
}
return $content;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment