Skip to content

Commit

Permalink
Default to the permalink if the url isn't defined
Browse files Browse the repository at this point in the history
This enables the block to be used in query loops, for example the Make homepage team grid
  • Loading branch information
adamwoodnz committed Oct 3, 2024
1 parent e2c61f7 commit 5fdbaeb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
19 changes: 18 additions & 1 deletion mu-plugins/blocks/link-wrapper/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,22 @@
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function init() {
register_block_type( __DIR__ . '/build' );
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => __NAMESPACE__ . '\render',
)
);
}

function render( $attributes, $content ) {
$wrapper_attributes = get_block_wrapper_attributes();
$link = isset( $attributes['url'] ) ? ' ' . $attributes['url'] : get_permalink();

return sprintf(
'<a href="%1$s" %2$s">%3$s</a>',
esc_url( $link ),
$wrapper_attributes,
do_blocks( $content )
);
}
3 changes: 3 additions & 0 deletions mu-plugins/blocks/link-wrapper/src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"padding": true
}
},
"dimensions": {
"minHeight": true
},
"typography": {
"fontSize": true,
"lineHeight": true
Expand Down
9 changes: 2 additions & 7 deletions mu-plugins/blocks/link-wrapper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ function Edit( { attributes, setAttributes } ) {

registerBlockType( metadata.name, {
edit: Edit,
save: ( { attributes } ) => {
const blockProps = useBlockProps.save();
return (
<a { ...blockProps } href={ attributes.url }>
<InnerBlocks.Content />
</a>
);
save: () => {
return <InnerBlocks.Content />

Check failure on line 59 in mu-plugins/blocks/link-wrapper/src/index.js

View workflow job for this annotation

GitHub Actions / Lint everything

Insert `;`
},
} );

0 comments on commit 5fdbaeb

Please sign in to comment.