Support

Account

Home Forums ACF PRO Shortcodes in Textarea?

Solved

Shortcodes in Textarea?

  • Hello,

    I am attempting to use some simple shortcodes for columns inside a textarea. I tried using all three options for line breaks and my shortcodes will still not output. They work fine with the Wysiwyg field type. Do I need to do anything specific to make them work?

    I am using ACF Pro by the way.

  • Text areas are not designed for shortcodes, but you could try something like:

    
    function text_area_shortcode($value, $post_id, $field) {
      if (is_admin()) {
        // don't do this in the admin
        // could have unintended side effects
        return;
      }
      do_shortcode($value);
      return $value;
    }
    add_filter('acf/load_value/type=textarea', 'text_area_shortcode', 10, 3);
    

    You could limit this to only a specific field by changing the hook to ‘acf/load_value/name={$field_name}’ or you could check $field inside the function for specific field names, or even add multiple add_filter() calls for different fields. Depends on how many text areas you want to filter and use shortcodes in.
    acf/load_value hook documentation

  • Hi guys

    Nice one @hube2. You can also use the format_value filter to avoid the is_admin() conditional:

    http://www.advancedcustomfields.com/resources/filters/acfformat_value/

    Cheers
    E

  • This is really nice, to allow editors to put emphasis and links in some textareas.

    Is there a way you could limit *which* shortcodes are available?

  • Not really… an least not easily, do_shortcode is indiscriminate. If it’s a registered shortcode WP will run it.

    It could be done with some work. You’d need to first build your own shortcode function/filter and use this instead of the WP hook. Look at the core function do_shortcode(). You’d pretty much need to do everything that function does, and possibly some of what the called functions do, yourself and build a “White List” to check shortcodes against to see if they should be run or not.

  • @hube2‘s code has a bug. Corrected code

    
    function text_area_shortcode($value, $post_id, $field) {
      if (is_admin()) {
        // don't do this in the admin
        // could have unintended side effects
        return;
      }
    
      return do_shortcode($value);
    }
    add_filter('acf/load_value/type=textarea', 'text_area_shortcode', 10, 3);
    
  • Revision:

    
    function text_area_shortcode($value, $post_id, $field) {
      if (is_admin()) {
        // don't do this in the admin
        // could have unintended side effects
    
        // revision: return $value because we don't want to miss on the textarea content
        return $value;
      }
    
      return do_shortcode($value);
    }
    add_filter('acf/load_value/type=textarea', 'text_area_shortcode', 10, 3);
    
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.