Share Article To

WordPress and ajax calls!!! Let me first explain you the situation. You have created one plugin and in this plugin one of your functionality need some ajax functionality mean you want to do something without refreshing the page like voting(like/dislike article), ajax sign in functionality.

After research alot and read several articles I understood that there are several ways to do a ajax call in WordPress like:
1: Use admin-ajax.php file for ajax call.
2: Include wp-load.php file in your ajax call file.
3: Include wp-config.php file with defining ‘SHORTINIT’ as TRUE.

Disadvantage of above ways:

– Site performance decreasing marginally due load the WP environment more than once.

– In first way is_admin() function will always get TRUE  even though you are in front-end.

Related links:
http://wordpress.stackexchange.com/questions/30638/determining-whether-its-a-ajax-call-from-front-end-or-from-back-end
http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side
http://ottodestruct.com/blog/2010/dont-include-wp-load-please/

Now let say one situation you are working on a project in which you need several ajax functionality for all these you have created one common .PHP file and include wp-load.php or wp-config.php file and do your job. Now in future someone else going to work on this project and want to create some new ajax functionality as the new developer not aware about your file s/he will create a new .PHP and include wp-load.php or wp-config.php file and do his/her own job. So now What is the status of your site performance? What is the time complexity of your site? Think !!!

So, is there any other way to perform a ajax call without decreasing the site performance. Yes, there is a nice, tricky and easy way to do this.

Let us do a test:

Step1: Login as admin.
Step2: Create a page(say title My Ajax Test).
Step3: Note this page slug here slug is my-ajax-test
or
Step3: Note this page ID.
Step4: Create one file with name page-PAGEID.php or page-PAGESLUG.php and put it in your current theme root directory.
Step5: Provide this created page’s url for ajax functionality.
Step6: Write code in the above created file, in this file you have access to all WordPress functions, for ajax functionality.
Step7: DONE 🙂

What are you thinking, is not a good and perfect way to do ajax call in WordPress?

Now what if you want to create a plugin in which you need to implement some ajax functionality and plugin needs to be portable?

In this case create a page using code and create a page-PAGEID.php file in your current theme root directory. Place you ajaxed php code in this file using PHP file handeling functions.

I have created one sample plugin which will help you to understand this more clearly.

How to use the above plugin:
– Install the plugin.

– Goto widget section.

– Drag the ‘WordPress Better Ajax’ widget to any sidebar.

– Goto front end and click on ‘Click Me’.

Advantages:

– This procedure will not decrease your site performance.

– In a single file you can do all your ajax calls.

– If you directly access this page then it will give you a message indicating that you are in  wrong place.

Please comment what you are think about this.

2 thoughts on “WordPress and Ajax calls

Leave a Reply

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