Support » Plugins » wp_localize_script adds a backslash | admin-ajax.php

  • Tosh


    wp_localize_script('cb_ajax_request', 'cbAjax', array('ajaxurl' => admin_url('admin-ajax.php')));

    And this how it looks when I do a view the page source:

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

    Here is a example I tried out. Just trying #1.

Viewing 3 replies - 1 through 3 (of 3 total)
  • as a workaround encode/decode the url?

    wp_localize_script('cb_ajax_request', 'cbAjax', array('ajaxurl' => urlencode(admin_url('admin-ajax.php'))));

    var ajaxurl = decodeURIComponent(cbAjax.ajaxurl);

    or something of the like.

    I’m having the exact same issue.
    If I follow any tutorial on the web it says to use that method to pull the ajax url but its constantly escaping the string. I even put in my own url string instead of the admin_url call so I know its wp_localize_script that’s causing it..

    I think it has to do with the html_entity_decode portion of it but cant figure out why everyone else isnt having the same issues…

    I found out what the issue is.
    The example we both went off of doesn’t mention when to fire the code so the both of us just put it into our plugin files.

    The issue is when its firing something isn’t loaded yet.
    If you wrap your code in a function and fire it with a add_action on wp_head it works as expected. Whatever needs to load by this point is loaded.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_localize_script adds a backslash | admin-ajax.php’ is closed to new replies.