Support

Account

Home Forums General Issues Escaping Fields exception

Solved

Escaping Fields exception

  • Hey,

    I’m using this code to escape all the fields:

    if ( ! function_exists( 'acf_kses_post' ) ) :
    
    	function acf_kses_post( $value ) {
    
    		if( is_array($value) ) {
    			return array_map('acf_kses_post', $value);
    		}
    
    		return wp_kses_post( $value );
    
    	}
    
    endif;
    
    add_filter('acf/update_value', 'acf_kses_post', 10, 1);

    Now I want to add a field to the footer which allows to add custom scripts. The problem is the <script> tag will removed. Is there a way to create a exception for a particular field without escaping?

  • 
    if ( ! function_exists( 'acf_kses_post' ) ) :
    
      function acf_kses_post( $value, $post_id, $field ) {
        
        if ($field['key'] == 'field_your_field_to_skip') {
          return $value;
        }
        if( is_array($value) ) {
          return array_map('acf_kses_post', $value);
        }
    
        return wp_kses_post( $value );
    
      }
    
    endif;
    
    add_filter('acf/update_value', 'acf_kses_post', 10, 4);
    
  • @hube2 thank you for helping me. I’ve tested this but it dosen’t work.

    My field is: get_field('footer_code', 'option');

    And the exception:

    if( $field['key'] == 'footer_code' ) {
            return $value;
        }
  • That’s the field name, change it to $field['name'] I generally use the field key just because it is not unique or may be altered in some cases (repeaters).

  • @hube2 you’re right 🙂 Thank you it works !!!

    With the field name:

    if( $field['name'] == 'footer_code' ) {
            return $value;
        }

    Or with the field key:

    $footer_code = get_field_object('footer_code', 'option');  
        $field_key = $footer_code['key'];  
          
        if( $field['key'] == $field_key ) {
            return $value;
        }
  • @hube2 now I’ve an other issue with all the fields. This is my code for the specific field exception:

    if ( ! function_exists( 'acf_kses_post' ) ) :
    
        function acf_kses_post( $value, $post_id, $field ) {
        
            // exception for specific fields
            $header_code = get_field_object('header_code', 'option');
            $header_field_key = $header_code['key'];
            $footer_code = get_field_object('footer_code', 'option');
            $footer_field_key = $footer_code['key'];
    
            
            if( $field['key'] == $header_field_key || $footer_field_key ) {
                return $value;
            }
            
            // escaping all fields  
            if( is_array($value) ) {
                return array_map('acf_kses_post', $value);
            }
    
            return wp_kses_post( $value );
    
        }
    
    endif;
    
    add_filter('acf/update_value', 'acf_kses_post', 10, 4);

    The issue is now I can add <script> tags to all other fields too. Is there anything missing in the code?

  • This is solving the issue:

    if ( ! function_exists( 'acf_kses_post' ) ) :
    
        function acf_kses_post( $value, $post_id = '', $field = '' ) {        
            
            // exception for option header and footer code
            $header_code = get_field_object('header_code', 'option');
            $footer_code = get_field_object('footer_code', 'option');
            
            if( $field['key'] == $header_code['key'] || $field['key'] == $footer_code['key'] ) {
                return $value;
            }
            
            // escaping all fields  
            if( is_array( $value ) ) {
                return array_map('acf_kses_post', $value);
            }
    
            return wp_kses_post( $value );
    
        }
    
    endif;
    
    add_filter('acf/update_value', 'acf_kses_post', 10, 4);
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.