jQuery AJAX Cannot Find Local File in WordPress Installation

enter image description here

I’m having trouble with a WordPress plugin I’ve been working on. A JS file is loaded as a resource with each page/post opened, which in turn has a request to load the contents of an HTML file.

Being that the page/post directories change frequently, I’m having a difficult time making the jQuery dynamically pin down the location of the file (even though it’s in the same location as the rest of the plugin resources).

An example:

  jQuery('body').append('<section id="asub00LOAD"></section>');

var url = jQuery(location).attr('hostname');
var dir = url + '/wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';

jQuery('#asub00LOAD').load(dir);

That was placing the whole URL path to the file after the local install (“root.com/CHEETOS/” in this case):

After which, I did this, which works fine for the root directory only:

  jQuery('body').append('<section id="asub00LOAD"></section>');

var dir = 'wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';

jQuery('#asub00LOAD').load(dir);

After you venture to another page, obviously the directory location is wrong again.

I tried to place some PHP into my JS file before so I could take advantage of the $plugins_url feature, but that became very convoluted and it’s hard to track any errors without a PHP console to work from…

I hope someone here will have a solution for me!

Read more here: jQuery AJAX Cannot Find Local File in WordPress Installation

Leave a Reply

Your email address will not be published. Required fields are marked *