function esc_js( $text ) {}

Escape single quotes, htmlspecialchar " < > &, and fix line endings.
Escapes text strings for echoing in JS. It is intended to be used for inline JS (in a tag attribute, for example onclick="…"). Note that the strings have to be in single quotes. The filter ‘js_escape’ is also applied here.


  • string $text: The text to be escaped.

Return values

returns:Escaped text.

Defined filters

  • js_escape
    apply_filters( 'js_escape', $safe_text, $text )

Source code

function esc_js( $text ) {

	$safe_text = wp_check_invalid_utf8( $text );

	$safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );

	$safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );

	$safe_text = str_replace( "\r", '', $safe_text );

	$safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );

	return apply_filters( 'js_escape', $safe_text, $text );



No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: