WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is my wp_enqueue_script and wp_localize_script - which i am using to run an Ajax call.

wp_enqueue_script( 'function', plugin_dir_url( __FILE__ ) . 'function.js', array( 'jquery', 'json2' ) );
wp_localize_script( 'function', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

While debugging this problem arose:

<script type='text/javascript'>
/* <![CDATA[ */
    var MyAjax = {"ajaxurl":"http:\/\/mydomain.com\/wp-admin\/admin-ajax.php"};
    /* ]]> */

As you can see my url is been escaped out and i have no idea why... is there a possible fix or alternative method for what i am trying to do?

Thanks in advance

share|improve this question
What is your end goal? If you use that in your ajax calls it will work just fine. – mor7ifer Feb 11 '12 at 17:30
@Danny, if it's fixed, please post what you did to fix it as an Answer so future visitors to the site can benefit. – EAMann Feb 15 '12 at 0:46
Sorry, it wasn't "fixed" as such, i just realised that escaped or not it still functions correctly. – Danny Feb 15 '12 at 1:22
up vote 5 down vote accepted

wp_localize_script() now uses json_encode() which means a multidimensional array will now work for the passed data. And, HTML entity decoding only applies to the first level of the array.

Better is an way to use json and default js possibilities from WP.

At first, i add the options from the database via script and json_encode to wp header:

    add_action( 'admin_enqueue_scripts', 'fb_print_scripts' );

    function fb_print_scripts() {
        global $current_screen;

        if ( isset( $current_screen -> id ) && ! in_array( $current_screen -> id, array( 'post', 'page' ) ) )

        if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
            $options = get_site_option( 'my_options_id' );
            $options = get_option( 'my_options_id' );

        if ( ! $options )
        <script type="text/javascript">
            var my_json_object = <?php echo htmlspecialchars( json_encode( $options ) ); ?>;

after this i read this data via javascript; the script include via wp_enqueue_script; the follow example init only in admin, you can change the hook without admin_ to include also in frontend.

add_action( 'admin_enqueue_scripts', 'fb_admin_enqueue_scripts' );

function fb_admin_enqueue_scripts( $where ) {

    if ( ! in_array( $where, array( 'post.php', 'post-new.php', ) )

    $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';

        self :: get_textdomain() . '_script',
        plugins_url( '/js/my_script' . $suffix. '.js', __FILE__ ),
        array( 'jquery', 'my_other_script' ),


now you can use the data from json inside your script, example

jQuery( document ).ready( function( $ ) {

    if ( typeof my_json_object == 'undefined' )

// debug in console of Browser
console.dir( my_json_object ); 

share|improve this answer

The issue comes from when you are firing the wp_localize_script call. Most likely you are just writing that code into your plugin which loads far too early.

I struggled with the problem for a while as well. If you wrap the code in a function and use add_action on wp_head it works as expected.

function ajaxurl() {
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-request', 'MyAjax', array(
// URL to wp-admin/admin-ajax.php to process the request
'ajaxurl'          => admin_url( 'admin-ajax.php' ),

// generate a nonce with a unique ID "myajax-post-comment-nonce"
// so that you can check it later when an AJAX request is sent
'postCommentNonce' => wp_create_nonce( 'myajax-post-comment-nonce' ),

add_action('wp_head', 'ajaxurl');
share|improve this answer
Shouldn't be wp_head, but wp_enqueue_scripts for scripts, styles that are loading on public facing pages. – kaiser Nov 4 '12 at 22:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.