Skip to content

Instantly share code, notes, and snippets.

@brettsmason
Last active June 16, 2023 10:39
Show Gist options
  • Save brettsmason/3bcb51bb32b568a74270d7d49666d598 to your computer and use it in GitHub Desktop.
Save brettsmason/3bcb51bb32b568a74270d7d49666d598 to your computer and use it in GitHub Desktop.
Link option for group block
<?php
add_action( 'render_block', [ $this, 'render_link_toolbar' ], 5, 2 );
/**
* Block link.
*
* @param mixed $block_content The block content.
* @param array $block The block data.
*
* @return mixed Returns the new block content.
*/
public function render_link_toolbar( $block_content, $block ) {
if ( isset( $block['blockName'] ) && ( in_array( $block['blockName'], [ 'core/group', 'core/column', 'core/cover' ] ) ) ) {
$attributes = $block['attrs'];
$block_name = explode( '/', $block['blockName'] );
if ( isset( $attributes['linkUrl'] ) && ! empty( $attributes['linkUrl'] ) ) {
$linked = '<a href="' . esc_attr( $attributes['linkUrl'] ) . '" class="wp-block-' . esc_attr( $block_name[1] ) . '__link"';
if ( isset( $attributes['linkOpensInNewTab'] ) && $attributes['linkOpensInNewTab'] ) {
$linked .= ' target="_blank"';
}
$linked .= '></a>';
$reg = '~(.*)</div>~su';
$subst = '${1}' . $linked . '</div>';
$block_content = preg_replace( $reg, $subst, $block_content );
}
}
return $block_content;
}
.wp-block-group {
position: relative;
// Pseudo link element.
&__link {
inset: 0;
max-width: unset !important;
position: absolute;
z-index: 5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment