0

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

1 Answer 1

0

the code needs some modification. see below.

/*****************************************************************************************************************************************************
 *
 *  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;
    }
    
    $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 );
    
    if ( empty( $href ) ) {
        return;
    }
    
    return '<a href="' . $href_before . $href . $href_after . '" title="' . $title . '" alt="' . $alt . '" target="' . $target . '" rel="' . $rel . '">' . $text . '</a>';
}
6
  • Please don't paste screenshots of your IDE for code samples; paste in the actual code samples. That will make it much easier for people to copy and paste the solution as needed.
    – Pat J
    Commented Jun 29, 2022 at 0:15
  • Also, as far as I can tell, your solution is to use {...} to delimit the if() block, but the OP's one-line if(...) return; is technically allowed in PHP. Using {...} makes the code more readable, but it won't change the behaviour of the code at all in this case.
    – Pat J
    Commented Jun 29, 2022 at 0:17
  • I tried to add code in the answer but it was not showing properly. So I upload these screenshot Commented Jun 29, 2022 at 18:14
  • The textarea for editing an Answer (or a Question) has a "Code Sample" button you can use to format your code snippets correctly. It looks like this: {} . Also, the editor uses Markdown; see here for more info.
    – Pat J
    Commented Jun 29, 2022 at 19:03
  • Thank you @AsiqurRahman It worked.
    – Rogger
    Commented Jul 5, 2022 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.