WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

In the .js script below, the ajaxPath reference breaks when the containing page is off the root directory of the site. Is there a workaround to make this path absolute?


    var dir = ContentEditableSettings.content_editable_url;
    var ajaxPath = 'wp-admin/admin-ajax.php'; //THIS RELATIVE PATH BREAKS OUTSIDE ROOT DIRECTORY

    function initContentEditable(){                 
            var parent = $(this).parent();
            var display = parent.css('display');
            $(this).css('display', display);

    function updateContentEditableTitle(e){
        var span = $(this);
        var data = new Object();
        data.pid = span.attr('data-pid');
        data.title = span.find('.saver').text();
        data.action = 'ce_update_title';
        data.security = ContentEditableSettings.nonce;
        $.post(ajaxPath, data, onContentSaved); //AJAXPATH BREAKS AS RELATIVE REF
share|improve this question
up vote 2 down vote accepted

As long as your wp-admin directory is still at the root of your site, you can just add a forward slash (/) to the URI. Alterntaively, you can use wp_localize_script() to pass in your site's URL or, if you are using WordPress's AJAX functions, the ajaxurl JS variable.

var ajaxPath = '/wp-admin/admin-ajax.php';


wp_enqueue_script( 'some_handle' );
$translation_array = array( 'ajaxPath' => admin_url( 'admin-ajax.php' ) );
wp_localize_script( 'some_handle', 'object_name', $translation_array );
share|improve this answer
Use admin_url instead of site_url. Like this: wp_localize_script( 'some_handle', 'object_name', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); – Otto Nov 2 '12 at 0:54
Thanks, @Otto! I was rolling that off the top of my head and forgot about admin_url(). – Joseph Leedy Nov 2 '12 at 12:20

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.