Home Forums Front-end Issues Shortcodes in text field


Shortcodes in text field

  • I have created a text field called ‘code’ and inside of it, I’ve put a shortcode;

    [formidable id=8]

    However on the front end, it doesn’t word and only shows the field as text rather than activating the shortcode.

    <?php echo do_shortcode( ' '. the_field('code') .' ' ); ?>

    Any ideas how to get it to work?

  • I found this on the web. Don’t know a lot about custom shortcodes, but I tested this and it worked.

    $shortcode = get_post_meta($post->ID,'YOUR_CUSTOM_FIELD_NAME',true);
    echo do_shortcode($shortcode); 
  • You can also add an acf/format_value filter, this exact example is covered here

    set the priority > 10

  • Thanks for all the comments, I solved it by adding the following to my functions.php

    add_filter('acf/format_value/type=textarea', 'do_shortcode');

  • no, both of these ‘solutions’ are needlessly complex and add_filter might be a potential security risk across every field rather than just the one you want to use

    a text field is plain text, therefore it can be directly utilized in php, which is what get_field() is for, rather than the_field() that wraps the data into its own echo function

    so it’s simply do_shortcode( get_field('FIELDNAME') );

    of course the best practice in case there’s no data and if there’s extra html around it is to check if it exists first

    if ( get_field('FIELDNAME') ) {
    	echo do_shortcode( get_field('FIELDNAME') );
  • Just because the documentation applies it to all textarea fields does not mean you must. Format value can also be used on just specific fields by including the field name or the field key in the filter name. You must remember that the examples are just that, examples, using the most simple form of the filter to get the point across.

    While you can do it in the code when you get the field using the filter allows you to do it once instead of at every instance where the field is used. It also allows you to easily alter the field long after the fact so that you don’t need to find every location where it is used in your theme and alter the code in every location.

    Here’s my take on security in this case. If the person that is doing the editing has access to the content editor and therefore are able to add shortcodes to the content then there is no extra security risk by allowing them to put shortcodes into text and textarea fields.

  • Simple as this code. No add code @ function.php

    Example: <?php echo do_shortcode(get_field(‘contact_form’)); ?>

  • add_filter(‘acf/format_value/type=text’, ‘do_shortcode’);
    The above worked for me perfectly for a text area with a shortcode.


  • Hi,

    I have been using this method for a while and it’s always worked, but I am now trying to load a shortcode from a custom field inside an ACF Gutenberg block but that doesn’t seem to work for me…

    I am simply storing the shortcode in my text field, when I call the text field inside my block php file and echo it using do_shortcode($custom_field_data); shortcodes aren’t being rendered.

    Any idea?

  • Sorry ignore my previous post, solved it now 🙂
    I was calling my custom field which wasn’t registered inside the block, instead it was registered outside the block in the side of the page and this can’t be called inside the block it seems.

  • Does this work if you have a shortcode in a WYSIWYG field that is in an ACF flexible content field? I’m not having any luck with it.

  • @relish27 WYSIWYG field do not need to be altered to apply shortcodes. WYSIWYG fields do this automatically.

  • Ah, duh! I think I was thinking I needed to because the Modern Tribe shortcode for The Events Calendar was just rendering as text and not working as a shortcode… so I was thinking I had to do something more.

    I’m realizing now that the shortcode isn’t working because we don’t have the PRO version of the Events Calendar Plugin:

    Thanks for the response.

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

You must be logged in to reply to this topic.