When user writes a comment and hits the submit button, WordPress reloads the page, before the comment is shown to the user.
Is there an Ajax-based solution that allows users to submit questions dynamically, without refreshing the whole page?
When user writes a comment and hits the submit button, WordPress reloads the page, before the comment is shown to the user.
Is there an Ajax-based solution that allows users to submit questions dynamically, without refreshing the whole page?
You must be logged in to post a comment.
Save the following javascrip file
wpse54189-ajax-comments.js
inside your plug-in folder – sayplugins/plug-in-name/js
(or if this must go your theme, in your theme folder).The above code uses a variable which stores the location of an ‘ajax’ loading image (see below). We need to give this variable the location of the image, which should be your plug-in folder, say
plugins/plug-in-name/img
, (or, again, if you must, your theme’s folder). We will use usewp_localise_script
.First though, we need register this script with WordPress, listing jquery as a dependency:
Then when you wish to enqueue the file:
wp_enqueue_script('wpse54189_ajax_comment')
– this will automatically print the variables and ensure jquery is loaded before hand.This can be placed inside template files, or for plug-ins, you can enqueue the script on a hook (for instance the
comment_form_before
hook):You can get a nice Ajax loading gif from here. Or simply use this:
All the JavaScript code was grabbed from wp-comment-master plugin (of course, I removed the unnecessary snippets â just kept what’s needed).
Yes, there is a lot of solutions, check it here: http://wordpress.org/extend/plugins/search.php?q=ajax+comments