I am facing a issue. I am using a snippet from github. It renders the URL with some attributes. However, when empty, the short code is still displaying and is not being hidden. Here it is:
/*****************************************************************************************************************************************************
*
* 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' => '_blank',
'rel' => 'nofollow',
'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 . '" rel="' . $rel . '">' . $text . '</a>';
}
Now, when the ACF field is empty it's rendering like this.
<a href="" title="" alt="" target="_blank" rel="nofollow">Read the complete story here.</a>
Kindly guide what I am missing here.
Thanks