I see that some people are having troubles using shortcodes in Ajax callbacks as apparently the Ajax instance is run in a different environment than usual.

Advices are more than one, many of them contradictory. You may be warned to declare wp.load.php to allow proper environment in the callback along with other includes.

Can someone clarify the legends and the truths about this argument?

  1. Is it a bad habit calling a shortcode from an Ajax callback, and if so why
  2. What are the requirements to gain a fully qualified wordpress environment inside an ajax callback. What are the bare minimum includes required to achieve this.
  3. What are the pitfalls to avoid and possible performance impacts

If someone may clarify these aspects the whole community may benefit from this :-)

  • I don't think the issue is environment (most of WordPress is loaded for AJAX requests), the problem is likely context. Shortcodes are usually created to operate on front-end requests- you can't enqueue js and css on AJAX requests, there's no main query object, etc.. – Milo Aug 18 '16 at 15:40
  • But some people are (successfully?) using this technique. What is the correct way of proceeding and what are are the drawbacks? – Riccardo Aug 18 '16 at 15:42
  • Some Shortcodes work fine, others don't, it depends on what each specific Shortcode does and how it does it. There isn't a fix that's going to work in all cases. – Milo Aug 19 '16 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.