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.


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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.