Skip to content

Instantly share code, notes, and snippets.

@dnavarrojr
Created August 7, 2018 22:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dnavarrojr/90db7597388fb844682c57d9379d9828 to your computer and use it in GitHub Desktop.
Save dnavarrojr/90db7597388fb844682c57d9379d9828 to your computer and use it in GitHub Desktop.
Create an HREF link with a shortcode using ACF fields
<?php
/*****************************************************************************************************************************************************
*
* Usage: [acf_href href_before="mailto:" href="acf:field_name" text="acf:field_name"]
*
* acf:fieldname will retrieve the value of the specified "fieldname" and use that.
* get:url_variable will grab a variable from the URL and use that.
*
* [acf_href href="acf:my_link" text="Link to web site"]
* [acf_href href_before="mailto:" href="acf:email_address" text="acf:user_name" post_id="get:post_id"]
*
*****************************************************************************************************************************************************/
/*---------------------------------------------------------------------------------------------------------------------------------------------------*/
function acf_field_from_string( $string, $post_id ) {
if ( substr( $string, 0, 4 ) == 'acf:' ) { // ACF field name
$string = get_field( substr( $string, 4 ), $post_id );
} elseif ( substr( $string, 0, 4 ) == 'get:' ) {
$string = $_GET[substr( $string, 4 ) ]; // $_GET variable
}
return $string;
}
/*---------------------------------------------------------------------------------------------------------------------------------------------------*/
add_shortcode( 'acf_href', 'tscpl_sc_acf_href' );
function tscpl_sc_acf_href( $atts ) {
extract( shortcode_atts( array(
'href_before' => '',
'href' => null,
'href_after' => '',
'text' => null,
'title' => null,
'alt' => null,
'target' => '_new',
'post_id' => null,
'post_type' => 'post', // default to POST if not specified
), $atts ));
if ( empty( $post_id ) ) { // inside the loop
$post_id = get_the_id();
} elseif ( is_numeric( $post_id ) == false ) { // slug
$post_id = acf_field_from_string( $post_id );
if ( empty( $post_type ) )
$post_type = get_post_type();
$post = get_page_by_path( $post_id, OBJECT, $post_type );
$post_id = $post->ID;
} else {
$post_id = (int) $post_id;
}
if ( empty( $href ) )
return;
$href = acf_field_from_string( $href, $post_id );
$href_before = acf_field_from_string( $href_before, $post_id );
$href_after = acf_field_from_string( $href_after, $post_id );
$text = acf_field_from_string( $text, $post_id );
$title = acf_field_from_string( $title, $post_id );
$alt = acf_field_from_string( $alt, $post_id );
return '<a href="' . $href_before . $href . $href_after . '" title="' . $title . '" alt="' . $alt . '" target="' . $target . '">' . $text . '</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment