WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an option on my plugin page where I load content into all the posts in the blog. Now this takes some time, esp when there are a log of posts.

Now the way is it set up now, when you press submit, the form success page is already presented, but you can see that the browser is still working.

How can I have the plugin wait until the whole loop is finished, and then present the error or success page.

share|improve this question
up vote 1 down vote accepted

You can only do this by "loading content into all the posts in the blog" asynchronously (in the background).

So, move the processing inside a function that you hook on wp_ajax_your_action_tag.

Then call this function with javascript by requesting WP to fire "your_action_tag". There's a example on the linked page...

If you want to display completion status, like a percentage bar, you might want to send an additional query variable besides the "action", like the offset from where the processing should continue. This offset would be increased in your processing function and sent back to the javascript, which calculates the status.

share|improve this answer
Great, thank you for the answer. I am busy with the exact thing you are suggesting here as I could not find any other option. But I was wondering 2 things, maybe you can help me out. 1) Is the build in ajax function secure? The function on the server side will do post manipulation, I have to be 100% sure the file can not be run outside the plugin. 2) Is it ok to use ajax in wordpress admin without fallback. It assumes everybody had javascript enabled. – Saif Bechan Nov 5 '11 at 12:46
1. There are three things you need to do: send a one-time generated nonce, which you verify in the ajax callback (this prevents submissions by mistake), check for proper user permissions, like current_user_can('update_core'), and sanitize any query variables if necessary. 2. Nobody has js disabled, but if you really want to handle that situation, do your stuff after a normal form submission (the behavior you described), but if javascript is enabled intercept the form submit call and do the ajax request instead – onetrickpony Nov 5 '11 at 12:54
Perfect. Accepted answer. – Saif Bechan Nov 5 '11 at 12:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.