Support

Account

Home Forums Backend Issues (wp-admin) How can I prevent ACF (or WordPress) from parsing and converting my shortcode to

Helping

How can I prevent ACF (or WordPress) from parsing and converting my shortcode to

  • Problem: ACF converts shortcode into HTML, but I want to store it as plain text

    Hi everyone!

    I’m using an ACF text area field (not a WYSIWYG editor), and I’m adding the following shortcode to it:
    [image id="1"]

    However, after saving the post, ACF automatically converts the shortcode into an HTML image tag, and what I see in the field is:

    <img class="img-fluid" loading="lazy" src="https://test.ru/wp-content/uploads/2023/03/summer-service.jpeg" srcset="..." sizes="..." alt="Employee" />

    What is not the problem:
    The field saves content correctly — if I add any other text next to the shortcode, it stays.

    Output using pre shows that the content is already stored as HTML, not the original shortcode.

    The field is a simple textarea, not a rich text editor.

    What I want:
    I want the shortcode [image id="1"] to be saved as is in the database — not converted into HTML.

    What I’ve tried:
    1. Rendering with do_shortcode() (works, but doesn’t prevent replacement):

    if (!empty($more_advantages)) { echo do_shortcode($more_advantages); // This just renders the shortcode }

    2. Trying to allow unsafe HTML output:

    add_filter('acf/shortcode/allow_unsafe_html', '__return_true'); add_filter('acf/the_field/allow_unsafe_html', '__return_true');

    3. Hooking into the ACF save process to escape the value:

    
    add_filter('acf/update_value/name=your_field_name', 'save_shortcode_as_text', 10, 3);
    
    function save_shortcode_as_text($value, $post_id, $field) {
    return esc_textarea($value); // Attempt to escape the value
    }
    

    The question:
    How can I prevent ACF (or WordPress) from parsing and converting my shortcode to HTML, and instead keep it as raw text?

    Any help is appreciated!

    If needed, I can also share the [image id="1"] shortcode definition and more info about theme/plugins.

  • By default shortcodes in acf textareas are not rendered, so you’ve already got some filter in place.

    I’ll asume that you use the filter 'acf/format_value/type=textarea'

    If you use the do_shorctode function or apply the_content filter inside without restricting it to the frontend only, then your shortcodes would be rendered in the admin textarea fields as well.

    To prevent this try to add the following restriction

    if (is_admin()) {
      return $value;
    }

    So your filter for acf textareas may look like this

    function process_acf_textarea( $value, $post_id, $field ) {
    
      if (is_admin()) {
        return $value;
      }
    
      return do_shortcode($value);
    
    }
    add_filter('acf/load_value/type=textarea', 'process_acf_textarea', 10, 3);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.