Created
September 16, 2025 12:57
-
-
Save tomfinitely/c836d3969271210ec89a5d5f5e008011 to your computer and use it in GitHub Desktop.
ACF Link Helper Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Returns a link from an ACF link field. | |
| * | |
| * @param array $link The ACF link field array containing URL, title, and target. | |
| * @param string $class Optional. Additional class(es) to add to the link. Default empty. | |
| * | |
| * @return string | |
| */ | |
| function get_acf_link( $link, $class = '' ) { | |
| if ( !is_array( $link ) || empty( $link['url'] ) ) { | |
| return ''; | |
| } | |
| // Extract and sanitize link attributes | |
| $url = esc_url( $link['url'] ); | |
| $title = esc_html( $link['title'] ?? '' ); | |
| $target = esc_attr( $link['target'] ?? '_self' ); | |
| $rel = ( $target === '_blank' ) ? 'noopener noreferrer' : ''; | |
| // Format the anchor tag | |
| return sprintf( | |
| '<a href="%s" %s target="%s" rel="%s">%s</a>', | |
| $url, | |
| $class ? 'class="' . esc_attr( $class ) . '"' : '', | |
| $target, | |
| esc_attr( $rel ), | |
| $title | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment