Skip to content

Instantly share code, notes, and snippets.

@GLWalker
Created May 2, 2023 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GLWalker/7d97a4113e970e302b09d1c90e97f0e4 to your computer and use it in GitHub Desktop.
Save GLWalker/7d97a4113e970e302b09d1c90e97f0e4 to your computer and use it in GitHub Desktop.
Function to convert WordPress Separator Block to a hook
if ( ! function_exists( 'separator_hookr' ) ) {
add_filter( 'render_block_core/separator', 'separator_hookr', null, 2 );
/**
* separator_hookr
*
* Method to add hooks using core/separator block
* seperator block must have a trigger class name of wps-action-hook
* followed by the desired hook name.
* If hyphonating use underscores not dashes
* Example class="wps-action-hook entry_content_hook"
*
* Works in conjunction with do_block_action() function.
*
* @param [type] $block_content
* @param [type] $block
* @return $block_content
*/
function separator_hookr( $block_content, $block )
{
/* exit if no class set */
if ( ! isset( $block['attrs']['className'] ) ) {
return $block_content;
}
/* Class is there, lets grab it */
$class = $block['attrs']['className'];
/* If no trigger class ( wps-action-hook ) exit */
if (! str_contains( $class, 'wps-action-hook' )) {
return $block_content;
}
/* we have our trigger class, so get the hook name, [1] */
$action = explode('wps-action-hook', $class)[1];
/* one more check, don't have to have it, but as long as the process is not to intensive lets roll with it */
if ( isset( $action ) && 'wps-action-hook ' . $action === $block['attrs']['className'] ) {
$block_content = do_block_action( $action );
}
return $block_content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment