Support

Account

Home Forums General Issues Message field, and wp_kses_allowed_html

Solving

Message field, and wp_kses_allowed_html

  • For some reason ACF message field is stripping away id and class attributes from an <input> tag when rendering.

    Tried adding those as allowed with the wp_kses_allowed_html filter, but it doesn’t seem to have any effect. Same method works fine for svg tags for example. Anyone have any ideas why that is happening?

    function acf_add_allowed_tags( $tags, $context ) {
    	
       if ( $context === 'acf' ) {
    	$tags['input'] = array(
    		'id' => true,
    		'class' => true,
    	);
       }
    
       return $tags;
    }
    
    add_filter( 'wp_kses_allowed_html', 'acf_add_allowed_tags', 10, 2 );
  • For message fields, you cannot insert input tags into the message. These will be stripped out when the field is saved.

    You need a two step process. The first you already did, and that is to allow the output of the input field.

    The second step is to use an acf/prepare_field filter for the message field and generate the input HTML in php and return it in $field[‘message’]

  • Apologies, that was silly of me. Using acf/prepare_field is actually what I’m doing, but the id and class attributes of input tag are being stripped out when the message field is rendered.

    I did get around this issue by using acf/render_field and echo the output I need instead. This seems to honor the allowed tags set by the wp_kses_allowed_html filter

  • I’m doing the same thing in one of my custom plugins, using the most recent version of ACF and it is working.

    What is the priority of your acf/prepare_field filter. Set it to something > 10 and try it again.

  • It’s preserving ID tags as well for you? Priority didn’t seem to have any effect, tried as high as 99 as well.

    I am however still running version 6.2.4, but I assume it should be working on that version as well. I will be updating and trying again soon.

    acf/render_field works well enough as a workaround for now though.

  • Yes it is. I have it set up to allow both input and textarea fields for a plugin. I use it to shoe input and textareas to allow copy and paste of shortcodes from the editor for my CPT.

    
    <?php 
    
    add_filter('acf/prepare_field/key=field_619663d258c1e', 'shortcodes_messages', 20);
    function shortcodes_messages($field) {
      global $post;
      if (is_a($post, 'WP_POST')) {
        ob_start();
        
        // I generate message here
        
        $field['message'] = ob_get_clean();
      }
      return $field;
    }
    
    add_filter('wp_kses_allowed_html', 'allow_input_in_acf', 20, 2);
    function allow_input_in_acf($tags, $context) {
      $attributes = array(
        'type',
        'disabled',
        'checked',
        'style',
        'name',
        'value',
        'class',
        'id',
        'style',
        'readonly'
      );
      if ('acf' === $context) {
        if (!isset($tags['input'])) {
          $tags['input'] = array();
        }
        foreach ($attributes as $attribute) {
          $tags['input'][$attribute] = true;
        }
        if (!isset($tags['textarea'])) {
          $tags['textarea'] = array();
        }
        foreach ($attributes as $attribute) {
          $tags['textarea'][$attribute] = true;
        }
      }
      return $tags;
    }
    
    
  • That is interesting. I must be experiencing some other plugin conflict then. Like mentioned, allowing svg tags works just as expected.

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

You must be logged in to reply to this topic.