WordPress.org

Ready to get started?Download WordPress

Forums

wp_localize_script adds a backslash | admin-ajax.php (4 posts)

  1. Tosh
    Member
    Posted 2 years ago #

    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:\/\/subdomain.mysite.com\/wp-admin\/admin-ajax.php"};
    /* ]]> */
    </script>

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

  2. secci
    Member
    Posted 2 years ago #

    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.

  3. dennissmolek
    Member
    Posted 2 years ago #

    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...

  4. dennissmolek
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic