ACF Link Field: Return Format, Target, and Safe Template Output

The Advanced Custom Fields (ACF) Link field can return either a URL string or an array containing url, title, and target. Template code must match the return format selected in the field settings. Mixing the two formats is one reason a link prints incorrectly or triggers PHP warnings.
The ACF Link field documentation shows both return formats. For reusable buttons and editorial links, the array format is usually more useful because editors can control the destination, visible text, and new-window preference in one field.
ACF Link field vs Page Link vs URL field
ACF has three link-related field types. Choose the one that matches what editors need to control:
- Link: editors set the URL, the link text, and whether the link opens in a new tab. It returns a Link Array (
url,title,target) or a Link URL string, depending on the Return Value setting. - Page Link: editors choose an existing post, page, or archive on the site. It returns the URL of the selected item, or an array of URLs when multiple selections are allowed.
- URL: the ACF URL field accepts a single web address. It returns that address as a string, so the template supplies the link text.
Use the Link field when editors need to control the visible text or the new-tab choice. Use Page Link for internal destinations whose URLs may change, and the URL field for a plain address such as a profile or document link.
Does ACF Page Link return a URL or a post ID?
The ACF Page Link field returns a destination URL, including when the selected item is a post. With multiple selections enabled it returns an array of URL strings. These are different from a Link Array with named keys. Do not pass a Page Link URL into a function that expects an integer post ID. An archive selection may not refer to any single post at all.
For a single-selection field named related_page, put the following in the relevant post template. The component owns the label, so choose one that actually describes the allowed destination. If editors need to pick the words as well as the destination, use a Link field configured for an array instead.
<?php
$selected = get_field( 'related_page' );
if ( is_string( $selected ) && '' !== $selected ) {
$safe_url = esc_url( $selected );
if ( '' !== $safe_url ) {
printf(
'<a href="%s">%s</a>',
$safe_url,
esc_html__( 'Read the project brief', 'site-theme' )
);
}
}
For multiple selections, loop over the returned array and check each destination before rendering. Give each link a distinct, meaningful label. If your component needs an editable label for each item, model the items as structured rows. The repeater alternatives guide explains when that extra structure is justified.
Recommended field configuration
- Create or edit the ACF Link field.
- Set Return Value to Link Array.
- Use clear field instructions so editors know what the link does.
- Require the field only when the template cannot work without it.
A field name such as profile_link is easier to maintain than a generic name such as url, especially when a component has several links.
Safe template pattern
<?php
$link = get_field( 'profile_link' );
$url = is_array( $link ) ? esc_url( $link['url'] ?? '' ) : '';
if ( '' !== $url ) :
$title = ! empty( $link['title'] ) ? $link['title'] : 'Visit profile';
$target = '_blank' === ( $link['target'] ?? '' ) ? '_blank' : '_self';
?>
<a class="profile-link" href="<?php echo $url; ?>"
target="<?php echo esc_attr( $target ); ?>"
<?php if ( '_blank' === $target ) : ?>rel="noopener noreferrer"<?php endif; ?>>
<?php echo esc_html( $title ); ?>
</a>
<?php endif; ?>
This pattern handles four responsibilities explicitly:
is_array()confirms that the field uses the expected return format.esc_url()prepares the destination for an HTML URL attribute.esc_attr()prepares attribute values.esc_html()prepares the human-readable link text.
ACF's get_field() reference states that values returned through the template API should be escaped for their output context. Escaping when saving is not a substitute for escaping when rendering.
Handle the link target (open in new tab) safely
The template does not print an arbitrary target. It permits _blank only when the field explicitly requests it and falls back to _self otherwise. When a link opens a new tab, rel="noopener noreferrer" prevents the opened page from receiving an unsafe reference to the original window.
Do not force every external link into a new tab. The target should follow the interaction requirement, and the visible text should make the destination understandable without relying on surrounding layout.
If the Link field returns only a URL
When the field's return format is Link URL, treat the value as a string and provide link text in the template:
<?php
$value = get_field( 'profile_link' );
$url = is_string( $value ) ? esc_url( $value ) : '';
if ( '' !== $url ) : ?>
<a href="<?php echo $url; ?>">Visit the team profile</a>
<?php endif; ?>
Do not index into $url['title'] in this configuration. It is a string, not an array.
Common failures
- The URL appears as the visible text. The template is printing the URL instead of the array's
titlevalue. - PHP reports an array-to-string conversion. The field returns an array, but the template tries to echo the whole value.
- The link disappears when the title is blank. Add meaningful fallback text or validate the editor input.
- The link works but is unsafe. Check contextual escaping and new-tab
relvalues. - The field works on one page only. Confirm the post ID or options-page context passed to
get_field().
Test the component, not only the PHP
Verify an empty field, an internal URL, an external URL, a new-tab link, a long title, and keyboard focus. Inspect the rendered HTML and test the link in the actual card, directory row, or button component where it will be used. That closes the gap between a syntactically valid snippet and a maintainable interface.
What if a translated link still opens the original page?
Inspect three values independently: the source field, the value on the translated post, and the rendered href. A translated label can sit beside an untranslated URL. If the return value is correct but the public markup is stale, check whether the template passes a fixed post ID or a page cache still serves the earlier HTML. See ACF and WPML translation preferences before changing every field to Copy.
A shared component can outlive its original field settings. Document its required field type, name, return format and fallback label next to the template. When an integration must handle both old and new records, custom WordPress development can preserve the contract while the data is migrated.