Link field
Purpose
A link in one control (URL, link text and "Open in a new tab") for buttons, calls to action and menus in themes.
Field type name
link
Settings
| Setting | Description |
|---|---|
| Instructions | Shown to editors below the field label. (max 2000 characters) |
| Allowed Schemes | Which kinds of absolute links are accepted. |
| Allow Relative Links | Accept links on this site such as /contact, #top or ?page=2. |
| Return Format | array (url, title, target) or url. |
| Translation | How the field behaves in other languages: translate, copy, copy_once or do_not_translate. Used on top-level fields when WPML is active. See WPML. |
| Width | Width on edit screens, to place fields side by side: 100 (full), 75, 66, 50, 33 or 25 (%). Phones always show full width. |
| Label per language | The label and instructions per language code while editing in that language. Set on the Translation tab when WPML is active. See WPML. |
A field needs at least one allowed scheme or relative links.
PHP return value
array{url: string, title: string, target: string} (or the URL with return format
url), null when empty.
Example
$link = cafe_get_field( 'cta' );
if ( $link ) {
printf(
'<a class="button" href="%s"%s>%s</a>',
esc_url( $link['url'] ),
'_blank' === $link['target'] ? ' target="_blank" rel="noopener"' : '',
esc_html( '' !== $link['title'] ? $link['title'] : $link['url'] )
);
}