admin-ajax.php vs .load() in WordPress

I’m developing a theme and I want to ajaxify each internal link in my theme. Uptill now, i’ve used admin-ajax.php method successfully for these actions …

1: Load More Posts for Blog with Ajax

Read More

2: Open Single Blog Post with Ajax

And i’ve used seperate .js scripts and ajax callback function for each of the above action. But the issue is if i want to ajaxify whole of my wordpress theme i’ll have to code many such scripts and callback functions for each internal link e.g author page, search page, default page template, custom page template … etc.

Now, i want to ask …

1: Is it the only way to ajaxify your whole site or if there is any better way? Cannot we use .load() method with less usage of code? (as in .load() method we only have to specify the URL to load irrespective of the fact that which page we are loading)

2: Why admin-ajax.php method is preferred vs .load() method?

Related posts

Leave a Reply

1 comment

  1. Is it the only way to ajaxify your whole site or if there is any better way?

    No, it’s not the only way. As you already know, you can indeed use .load(). As to what’s better? That’s wide open to debate and depends on personal preference, requirements, circumstances etc.

    Cannot we use .load() method with less usage of code?

    Less code, maybe. But does that always mean the “right” way? No.

    Why admin-ajax.php method is preferred vs .load() method?

    Hitting the AJAX handler instead of an asynchrous page load is less intensive on the server, and the HTTP response size is typically smaller (you’re only sending back the data you need, not a whole HTML document).

    Now that’s not neccessarily a winning case for admin-ajax.php, but it’s how I’d argue that one could reason it is “the preferred way”.

    Ultimately, it’s up to you. I agree that .load() would be easier, so I’d say go for it, and don’t get too hung up on the “right way” deemed by others without considering your own specific circumstances.