• Resolved Blutarsky

    (@blutarsky)


    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
    4. why

    If someone may clarify these aspects the whole community may benefit from this ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’ve not attempted shortcodes via AJAX myself, so some of this is speculative. Shortcodes are normally processed by core code hooked into ‘the_content’ filter. Since AJAX callbacks directly output content, shortcodes are not expanded. However, if the callback were to output content by calling the_content(), I would expect shortcodes to be processed.

    What one can do is explicitly run AJAX output through do_shortcode(), as in:
    echo do_shortcode( $content );

    Including wp-load.php is never the proper way to load the WP environment. You can get away with it if doing so is only for your own specific installation and your code will not be used elsewhere. This is because on any given installation, code cannot know the location of wp-load.php. Not only can WP be loaded in any sub-folder, but the location of /wp-content/ where your code should reside can be moved from it’s usual position below WP root. There’s no way to include wp-load.php that will work in all possible WP configurations.

    In any case, if AJAX is done correctly, by running requests through /wp-admin/admin-ajax.php, then the environment is already loaded and there is no reason to include wp-load.php. Thus, if someone is finding it necessary to include wp-load.php, they are Doing It Wrong? twice over. One by including wp-load.php, and again by not running requests through admin-ajax.php.

    Thread Starter Blutarsky

    (@blutarsky)

    Thank you mate for clarifying. Another important aspect is that in this scenario using shortcodes (or functions behind shortcodes) has no guarantee of success: execution may fail just because the flow in the code is being altered depending on is_admin() (Ajax code is always run with is_admin() set to true. Correct?

    Moderator bcworkz

    (@bcworkz)

    Yes, that’s right. Good point! While illogical for front end AJAX, considering AJAX requests are new requests as far as the server is concerned, is_admin() returning true kinda makes sense in a convoluted way. Typically devs develop AJAX routines from scratch and would of course not use is_admin() in their code if they are working on front end AJAX.

    That all flies out the window for us hackers modifying other’s code, especially when trying to implement shortcodes that were never intended for use in AJAX callbacks. It means we need to get even more creative in such situations. In extreme cases it may mean modifying the underlying shortcode handler or its dependencies. At some point it may make more sense to develop our own routines rather than working off existing code.

    Thread Starter Blutarsky

    (@blutarsky)

    ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Ajax callback and shortcode functionality’ is closed to new replies.