Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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

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

    function initContentEditable(){                 
        $('.contenteditable.furniture').blur(updateContentEditable);
        $('.contenteditable.custom').blur(updateContentEditableCustom);
        $('.contenteditable.title').blur(updateContentEditableTitle);
        $('.contenteditable').each(function(){
            var parent = $(this).parent();
            var display = parent.css('display');
            $(this).css('display', display);
        });
    }   

    function updateContentEditableTitle(e){
        e.preventDefault();
        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
add comment

1 Answer

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

Or

<?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 Nov 2 '12 at 12:20
add comment

Your Answer

 
discard

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.