Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Make AJAX theme work with plugins WordPress


I'm developing a theme that loads all the page and post content with AJAX so the page doesn't reload.

All the theme is working fine, but when I try to run a plugin that use javascript, it doesn't load it's javascript inside the ajax content, it only loads javascript on homepage. This means that the theme is not compatible with any plugin that uses javascript.

Do you know any filter, function or anything to load javascript of third-hand plugins when I load a page with AJAX?

I tried to copy the plugin javascript inside my jQuery AJAX function and it works fine, but I don't want people to do that every time they install a plugin...

Thanks in advance!

Answers (5)


Martin Pham answers:

try this function for wordpress load script

add_action('wp_enqueue_scripts', 'load_scripts_method');
function load_scripts_method() {
wp_enqueue_script( 'customscript', 'custom-script-path', array( 'jquery' ));

[[LINK href=""]][[/LINK]]
[[LINK href=""]][[/LINK]]

OR using lazyload script (call back)

function lazyscript(url,success){
var script=document.createElement("script");
var head=document.getElementsByTagName("head")[0],
script.onload=script.onreadystatechange = function(){
if ( !done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") ) {
script.onload = script.onreadystatechange = null;
// example
var m = document.createElement("script");
m.src = 'child-jquery-plugin';


Jatin Soni answers:

Check your jquery is coded with jquery no-conflict

Andreu Llos comments:

As my explanation says, I guess that there isn't any jQuery Conflict. jQuery is not running when I load a page with AJAX because it was already loaded previously on homepage.


Arnav Joy answers:

please have a look in this article

Andreu Llos comments:

Thank you. I already know this article and sometimes I have used the admin-ajax.php, but in this case, the problem is that the theme is an adaptation of a html and it loads the URL using a $.load('url'). I'm not using admin-ajax.php here and neither the corresponding ajax wordpress actions. Is this a problem?


Plugarized answers:

Can you please post a link to your website

Andreu Llos comments:

Of course, here you have


Christianto answers:


I check your site and found out that you are using .load with selector expression to get the content.
( I hope is not wrong code to load the content :D )

$mainContent.load(<strong>History.getState().url+' .item-wrapper'</strong>, function(response, status, xhr){
// your code..

When using ajax.load with selector expression (<strong>History.getState().url+' .item-wrapper'</strong>), all js files are removed so it won't be execute

you can read the explanation in [[LINK href=""]]jQuery.load doc on Script Execution section[[/LINK]]
<blockquote>If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed</blockquote>

If you need to execute all js in the target page, You can use .load function without selector,
or could be by creating temporary iframe that is attach to dom but hidden, so you can access part of the page with [[LINK href=""]]jQuery.contents()[[/LINK]], and delete it if not needed..

Hope this help..