Skip to content

Commit 5ac80b3

Browse files
committed
Default to the permalink if the url isn't defined
This enables the block to be used in query loops, for example the Make homepage team grid
1 parent e2c61f7 commit 5ac80b3

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

mu-plugins/blocks/link-wrapper/index.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,29 @@
2020
* @see https://developer.wordpress.org/reference/functions/register_block_type/
2121
*/
2222
function init() {
23-
register_block_type( __DIR__ . '/build' );
23+
register_block_type(
24+
__DIR__ . '/build',
25+
array(
26+
'render_callback' => __NAMESPACE__ . '\render',
27+
)
28+
);
29+
}
30+
31+
/**
32+
* Renders the Link Wrapper block.
33+
*
34+
* @param array $attributes Block attributes.
35+
* @param string $content Block content.
36+
* @return string Rendered block HTML.
37+
*/
38+
function render( $attributes, $content ) {
39+
$wrapper_attributes = get_block_wrapper_attributes();
40+
$link = isset( $attributes['url'] ) ? ' ' . $attributes['url'] : get_permalink();
41+
42+
return sprintf(
43+
'<a href="%1$s" %2$s>%3$s</a>',
44+
esc_url( $link ),
45+
$wrapper_attributes,
46+
do_blocks( $content )
47+
);
2448
}

mu-plugins/blocks/link-wrapper/src/block.json

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"padding": true
3131
}
3232
},
33+
"dimensions": {
34+
"minHeight": true
35+
},
3336
"typography": {
3437
"fontSize": true,
3538
"lineHeight": true

mu-plugins/blocks/link-wrapper/src/index.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function Edit( { attributes, setAttributes } ) {
2424
<>
2525
<InspectorControls key="setting">
2626
<PanelBody title={ __( 'Link destination', 'wporg' ) }>
27+
<p>{ __( 'Defaults to the permalink if not set.', 'wporg' ) }</p>
2728
<TextControl
2829
label={ __( 'Link destination', 'wporg' ) }
2930
hideLabelFromVision
@@ -55,12 +56,7 @@ function Edit( { attributes, setAttributes } ) {
5556

5657
registerBlockType( metadata.name, {
5758
edit: Edit,
58-
save: ( { attributes } ) => {
59-
const blockProps = useBlockProps.save();
60-
return (
61-
<a { ...blockProps } href={ attributes.url }>
62-
<InnerBlocks.Content />
63-
</a>
64-
);
59+
save: () => {
60+
return <InnerBlocks.Content />
6561
},
6662
} );

0 commit comments

Comments
 (0)