Skip to content
Block Controls

Link

Link control provides a comprehensive link input with title, target, nofollow, and CSS classes options. Unlike the basic URL control, it stores multiple link attributes as an object, giving you full control over link behavior and styling.

Link Control
Link control with advanced options

Control Settings

  • Show Title Field - Allow users to set a custom title/text for the link
  • Show "Open in new tab" Toggle - Allow users to toggle opening link in new tab
  • Show "Mark as nofollow" Toggle - Allow users to add nofollow attribute to the link
  • Show CSS Classes Field - Allow users to add custom CSS classes to the link
  • Link Suggestions - Choose which content types appear in search results (Automatic, Disable, or specific post type)
  • Show Initial Suggestions - Display suggestions immediately on focus
  • Rich Preview - Show link preview with site title and image

Output Format

The Link control returns an object with the following properties:

PropertyTypeDescription
urlstringThe link URL
titlestringCustom link title/text
opensInNewTabbooleanWhether to open in new tab
nofollowbooleanWhether to add nofollow attribute
cssClassesstringCustom CSS classes

Usage Examples

PHP
<?php
$link = $attributes['control_name'];
if ( $link && $link['url'] ) :
    $target = $link['opensInNewTab'] ? ' target="_blank"' : '';
    $rel_parts = [];
    if ( $link['opensInNewTab'] ) {
        $rel_parts[] = 'noopener';
        $rel_parts[] = 'noreferrer';
    }
    if ( $link['nofollow'] ) {
        $rel_parts[] = 'nofollow';
    }
    $rel = $rel_parts ? ' rel="' . implode( ' ', $rel_parts ) . '"' : '';
    $classes = $link['cssClasses'] ? ' class="' . esc_attr( $link['cssClasses'] ) . '"' : '';
    $title = $link['title'] ?: __( 'Click here', 'your-text-domain' );
?>
    <a href="<?php echo esc_url( $link['url'] ); ?>"<?php echo $target . $rel . $classes; ?>>
        <?php echo esc_html( $title ); ?>
    </a>
<?php endif; ?>
PHP
<?php
$link = $attributes['control_name'];
if ( $link && $link['url'] ) :
    $attrs = [
        'href'  => esc_url( $link['url'] ),
        'class' => 'button ' . esc_attr( $link['cssClasses'] ?? '' ),
    ];
 
    if ( $link['opensInNewTab'] ) {
        $attrs['target'] = '_blank';
        $attrs['rel'] = 'noopener noreferrer';
    }
 
    if ( $link['nofollow'] ) {
        $attrs['rel'] = ( $attrs['rel'] ?? '' ) . ' nofollow';
    }
 
    $attr_string = '';
    foreach ( $attrs as $key => $value ) {
        $attr_string .= sprintf( ' %s="%s"', $key, trim( $value ) );
    }
?>
    <a<?php echo $attr_string; ?>>
        <?php echo esc_html( $link['title'] ?: __( 'Learn More', 'your-text-domain' ) ); ?>
    </a>
<?php endif; ?>

Handlebars Usage

Basic Link
{{#if control_name.url}}
  <a href="{{control_name.url}}"
     {{#if control_name.opensInNewTab}}target="_blank" rel="noopener noreferrer"{{/if}}
     {{#if control_name.cssClasses}}class="{{control_name.cssClasses}}"{{/if}}>
    {{#if control_name.title}}
      {{control_name.title}}
    {{else}}
      Click here
    {{/if}}
  </a>
{{/if}}

Post Meta

Post Meta
<?php
$link = get_lzb_meta( 'control_meta_name' );
if ( $link && ! empty( $link['url'] ) ) {
    $target = ! empty( $link['opensInNewTab'] ) ? '_blank' : '_self';
    $rel = [];
 
    if ( ! empty( $link['opensInNewTab'] ) ) {
        $rel[] = 'noopener';
        $rel[] = 'noreferrer';
    }
    if ( ! empty( $link['nofollow'] ) ) {
        $rel[] = 'nofollow';
    }
 
    printf(
        '<a href="%1$s" target="%2$s"%3$s%4$s>%5$s</a>',
        esc_url( $link['url'] ),
        esc_attr( $target ),
        $rel ? ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"' : '',
        ! empty( $link['cssClasses'] ) ? ' class="' . esc_attr( $link['cssClasses'] ) . '"' : '',
        esc_html( $link['title'] ?: __( 'Read More', 'your-text-domain' ) )
    );
}
?>
  • Always use esc_url() when outputting URLs to prevent XSS attacks.
  • When opensInNewTab is true, always include rel="noopener noreferrer" for security.
  • The Link control stores data as an object, so access properties using array syntax in PHP.

Was this article helpful?

Copyright © 2025 Lazy Blocks.