WordPress.org

Make WordPress Core

Ticket #29910: esc_json_encode.php

File esc_json_encode.php, 567 bytes (added by enej, 6 years ago)

contains esc_json_encode function

Line 
1<?php 
2
3/**
4 *  Escaping for HTML attributes use this instead of esc_attr( json_encode( ) )
5 *
6 * @param  array or object $data array or object to be escaped
7 * @return properly escaped data
8 */
9function esc_json_encode( $data ) {
10
11        if ( defined( 'JSON_HEX_AMP' ) ) { 
12                // This is nice to have, but not strictly necessary since we use _wp_specialchars() below
13                $data = json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); 
14        } else { 
15                $data = json_encode( $data ); 
16        } 
17        return _wp_specialchars( $data, ENT_QUOTES, false, true );
18}