Support

Account

Home Forums Backend Issues (wp-admin) Using Custom Shortcodes in ACF Fields

Solved

Using Custom Shortcodes in ACF Fields

  • Hi, I’ve created some custom shortcodes in my functions.php file so I can use them in the WP backend. They work perfectly fine when used in post/page content, but not when used in custom fields. Is there a way I can make them work?

    This is what I currently have in my functions.php file:

    function an_example_shortcode() {
        global $foo;
        return $foo;
    }
    add_shortcode('an-example-shortcode', 'an_example_shortcode');

    So I’d like to be able to just type [an-example-shortcode] into a custom field and have it output the $foo variable, just as it does in page/post content.

    Please bear in mind that I will have quite a few of these shortcode functions, so something efficient/easily reusable would be ideal.

    Thanks – really appreciate your time.

  • Hi @ste9890

    I think that the way to solve this depends on how your ACF fields are being used..

    If they are exclusively for shortcodes you can use the do_shortcode() function to output the information like this:

    
    echo do_shortcode(get_field('myfield'));
    

    If they can contain a regular textstring as well as shortcodes you could propably make use of the_content filter:

    
    echo apply_filters('the_content', get_field('myfield'));
    

    Please note that this will also apply p tags around regular strings.

  • Hi Jonathan,

    Thanks for your help. Your suggestion doesn’t seem to be working unfortunately.

    This is what I have in my custom field:
    Lorem ipsum dolor: [an-example-shortcode]

    Then in my template file I now have this:

    while ( have_rows('records') ) : the_row();
        echo apply_filters('the_content', get_sub_field('record'));
    endwhile;

    This is simply outputting “Lorem ipsum dolor: [an-example-shortcode]”

    Any other suggestions?

    Thanks again

  • Okay. Yeah it would not work if you have mixed shortcode and string content.

    Strangely I think your current code should work.. the_content filter should change any shortcode into it’s output.. are you 100% sure you declare your shortcode correctly in the field?

    Another solution would be to have two fields. One where you can put in any string and another which would contain only the shortcode.

    That way my first example should work.

  • Jonathan, you’re quite right! I must have been too quick to dismiss it last time I tried as I’ve had another go and got it working. Again, huge thanks for your help!

    Sorry to be cheeky but I have one last issue I’ve been struggling with that hasn’t received any responses yet: http://support.advancedcustomfields.com/forums/topic/order-repeater-field-rows-by-date/ I don’t suppose you’d be able to help with that?

  • No problem!
    Sometimes you miss the tiniest things 🙂

    I’ll have a look.

  • I’ve also found that for a shortcode-only field, you should make sure “Formatting” for the field is set to “Convert HTML into tags” and not “No formatting”.

    I had a text field set to:

    [contact-form-7 id=”3650″ title=”My Contact Form”]

    and when using:

    echo do_shortcode(get_field('myfield'));

    it would display as:

    [contact-form-7 404 “Not Found”]

    It was only when I changed “Formatting” to “Convert HTML into tags” that the shortcode was expanded properly.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Using Custom Shortcodes in ACF Fields’ is closed to new replies.