Support

Account

Home Forums Gutenberg Down & Dirty Core Group Block Hyperlinks

Helping

Down & Dirty Core Group Block Hyperlinks

  • I am attempting to create a basic, accessible link wrapper for core WP group blocks. There are plugins available that provide this functionality, so I thought why not just use ACF. However, from what I am seeing, it does not appear that an HTML A tag can be used to wrap InnerBlocks. Using the code below, InnerBlocks is output outside the initial A tag, and multiple elements within InnerBlocks are repeatedly wrapped with the same A tag. My goal is to create linked cards (group blocks that contain images, text, misc, etc).

    Is there a requirement to only wrap InnerBlocks with a DIV? Or is my code not correct for the proper output? Thanks for any assistance.

    
    // Block anchor/ID.
    $block_id = 'group-link-' . $block['id'];
    
    // Check for custom anchor.
    if (!empty($block['anchor'])) {
        $block_id = esc_attr($block['anchor']);
    }
    
    // Block class.
    $className = 'block-group-link';
    
    // Check for custom classes.
    if (!empty($block['className'])) {
        $className .= ' ' . esc_attr($block['className']);
    }
    
    // Allowed inner blocks (array).
    $allowed_blocks = ['core/group'];
    
    // ACF fields.
    $link = get_field('group_link');
    
    $link_url = $link['url'];
    $link_title = $link['title'];
    $link_target = $link['target'] ? $link['target'] : '_self';
    ?>
    
    <a>" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>" role="link"
        aria-description="<?php echo esc_html($link_title); ?>" class="<?php echo $className; ?>">
        <InnerBlocks allowedBlocks="<?php echo esc_attr(wp_json_encode($allowed_blocks)) ?>" />
    </a>
    
  • I just realized my code was garbled when pasting. The correct code is:

    <a href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>" role="link"
        aria-description="<?php echo esc_html($link_title); ?>" class="<?php echo $className; ?>">
        <InnerBlocks allowedBlocks="<?php echo esc_attr(wp_json_encode($allowed_blocks)) ?>" />
    </a>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.