• Hi,

    I’ve seen this issue being addressed over and over again, but i haven’t come across a 100% working solution for everyone that is trying to create custom AJAX calls on content that contains shortcodes. Some people suggest making the AJAX call trough wordpress frame (trough admin ajax)

    https://codex.www.ads-software.com/AJAX_in_Plugins

    while others say its better to include core wordpress functionalities in your own file (including wp-load.php in your php file)

    https://www.ads-software.com/support/topic/running-shortcode-inside-ajax-request?replies=9#post-5324569

    I have tried both versions, but actually none of the mentioned techniques work – meaning, the shortcodes aren’t parsed.

    F.ex. i’m trying to ‘load more posts’. Content in the posts is created with DIVI builder. Shortcodes in the response are however, unparsed.
    The only way the response is correct is in the second example, but if i include the wp-blog-header.php instead of wp-load.php. But if i do include the wp-blog-header.php, i get a 404 error (im guessing because of security reasons, cause the actual response is in this case correct; still not displayed as its a 404).

    Is there a reliable way to achieve displaying shortcodes trough AJAX? I mean, it can’t be THAT hard if there are thousand AJAX plugins that work correct on this principle.

Viewing 2 replies - 1 through 2 (of 2 total)
  • it can’t be THAT hard if there are thousand AJAX plugins that work correct on this principle.

    Then find one of these plugins and see how it works, even tell us here what you found.
    Just about everything I needed to know for writing my plugins I learnt this way.

    Moderator bcworkz

    (@bcworkz)

    Including or requiring wp-load.php or wp-blog-header.php is a very poor practice. Doing so will fail to work properly on many WP installations. If you are coding only for your specific installation, it’ll work OK, but you are still better off doing it the right way to start with.

    With AJAX, the only correct WP way is to go through admin-ajax.php. If that is not working for you then you’ve done something wrong. Essentially, shortcodes are only automatically expanded when the template tag the_content() (or one of its direct relatives) is used. To expand shortcodes in other situations, run the content through do_shortcode().

    If you wish to expand shortcodes for existing AJAX calls from some other plugin or theme, you need to identify what action tag is being sent in the AJAX request. That tag is used to construct an action hook which you can hook into in order to run content through do_shortcode(). You’ll also need to determine what format the content is in when it reaches your action callback so you can correctly process it. Return the data in the same format as you received it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcodes in ajax – again’ is closed to new replies.