Support

Account

Home Forums General Issues ACF within a Shortcode

Solved

ACF within a Shortcode

  • Hi,

    I’m trying to pass an Advanced Custom Fields variable through to a shortcode that’s been placed within a custom template, but haven’t had any luck so I’m not sure if it can be done.

    More specifically, I have a plugin that displays details of an Amazon product if I enter the ASIN into the shortcode. This works fine in a post, but I’d like to create a custom template that only requires the user to enter the ASIN. I’ve created a custom hook and added the shortcode. It continues to work fine if I hardcode the ASIN. However, I’d like the Advanced Custom Fields variable (asin) entered by the user to be used within the shortcode.

    As an example, let’s say this is the standard shortcode format:

    [asin="B00X4WHP5E"]

    With “asin” as my Advanced Custom Field, I’ve tried to use the code below, but it doesn’t work. My php skills aren’t strong enough to know if this isn’t possilbe, or if I’m just not formatting it properly:

    <?php echo do_shortcode( '[asin="the_field('asin')"]' ); ?>

    Any help would be appreciated.

    Thanks!

  • Hi there…

    I was able to get something like the following to work:

    
    <?php
    $asin = get_field('asin');
    echo do_shortcode( "[asin=$asin]" );
    ?>
    

    Hope that helps!

  • My actual shortcode was a little more complex than my example, so I couldn’t modify your suggestion to work, but it put me on the right track. Ultimately, I got it to work with something like this:

    <?php
    $asin = get_field('asin');
    $text1 .= '[asin="'.$asin.'"]';
    $text2 .= do_shortcode($text1);
    echo $text2;
    ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘ACF within a Shortcode’ is closed to new replies.