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

  • 2
    What is your end goal? If you use that in your ajax calls it will work just fine.
    – mor7ifer
    Feb 11, 2012 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, 2012 at 0:46
  • Sorry, it wasn't "fixed" as such, i just realised that escaped or not it still functions correctly.
    – Danny
    Feb 15, 2012 at 1:22

2 Answers 2


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 ); 


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');
  • Shouldn't be wp_head, but wp_enqueue_scripts for scripts, styles that are loading on public facing pages.
    – kaiser
    Nov 4, 2012 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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