Advanced Link
An enhanced version of ACF's built-in Link field. Adds a CSS class input and lets you toggle which sub-fields are visible. Can return either an associative array or a ready-to-render HTML <a> tag.
Settings
| Setting | Default | Description |
|---|---|---|
| Show Link Text | true | Display the “Link Text” input. |
| Show Open in New Tab | true | Display the target=“_blank” checkbox. |
| Show CSS Class | false | Display an optional CSS class input. |
| Return Format | array | array · html_a |
Return values
return_format: 'array'
Returns an associative array with all active sub-fields.
$link = get_field('cta_link');
// [
// 'url' => 'https://example.com/pricing',
// 'title' => 'See pricing',
// 'target' => '_blank',
// 'class' => 'btn btn-primary',
// ]
target is '_blank' when checked, or an empty string when unchecked.
return_format: 'html_a'
Returns a complete <a> element string. Empty if no URL is set.
$link = get_field('cta_link');
// '<a href="https://example.com/pricing" target="_blank" class="btn btn-primary">See pricing</a>'
echo $link; // safe to echo — values are escaped internally
Usage
Rendering from the array format
$link = get_field('hero_cta');
if ($link && $link['url']) {
$target = $link['target'] ? ' target="' . esc_attr($link['target']) . '"' : '';
$class = $link['class'] ? ' class="' . esc_attr($link['class']) . '"' : '';
$title = $link['title'] ?: $link['url'];
echo '<a href="' . esc_url($link['url']) . '"' . $target . $class . '>';
echo esc_html($title);
echo '</a>';
}
Using the HTML return format
$link_html = get_field('card_link');
if ($link_html) {
echo $link_html; // already escaped
}
Adding extra attributes after the fact
$link = get_field('nav_cta');
if ($link && $link['url']) {
$classes = array_filter(['btn', 'btn-outline', $link['class'] ?? '']);
$classes_str = implode(' ', $classes);
printf(
'<a href="%s"%s class="%s" aria-label="%s">%s</a>',
esc_url($link['url']),
$link['target'] ? ' target="_blank" rel="noopener"' : '',
esc_attr($classes_str),
esc_attr($link['title'] ?? ''),
esc_html($link['title'] ?: 'Learn more')
);
}
Repeater of links
if (have_rows('footer_links')) {
echo '<ul class="footer-nav">';
while (have_rows('footer_links')) {
the_row();
$link = get_sub_field('link');
if ($link && $link['url']) {
echo '<li><a href="' . esc_url($link['url']) . '">';
echo esc_html($link['title'] ?: $link['url']);
echo '</a></li>';
}
}
echo '</ul>';
}