0

I am loading a php file containing echo do_shortcode('[contact-form-7 id="568"]'); using jQuery .load() but the shortcode returns nothing.

I have seen this question and answer and understand that do_shortcode() isn't available: Why might a plugin's 'do_shortcode' not work in an AJAX request?

But I'm afraid I can't for the life of me figure out if the solution is relevant to my setup.

Hoping someone can help shed some light.

Thank you.

script.js

$.post(themeurl+ 'save_image.php', { base64_image: ProductDataURL }, function(data) {
    if(data) {
        $( '#popup-download div' ).load( themeurl+ 'downloadform.php' );
    }
});

downloadform.php

<?php
if ( function_exists( 'do_shortcode ' ) )  {
        echo "do_shortcode exists";
} else {
        echo "do_shortcode doesn't exist";  // this gets echoed
};
echo do_shortcode('[contact-form-7 id="568"]'); // nothing
?>
1

From what you have shown, downloadform.php never loads WordPress, hence naturally do_shortcode() is not available.

You need to implement your AJAX using this Codex article: http://codex.wordpress.org/AJAX_in_Plugins

Another alternative I would suggest is loading the contact form in a hidden div during the initial page load and then display the hidden div when users click whatever makes the form pop up. That way you don't need any AJAX at all.

  • Thanks user2572827. The alternative, loading with page load, isn't appropriate as I have a dynamic field in the form that is updated after the image is created (see line 1 of code above). So I am reading and trying my best to understand the content of the link you sent :) – Caroline Elisa Sep 1 '14 at 16:08
  • @CarolineElisa, if you are looking for an easy way to expose a JSON endpoint from WordPress, check out this blog post: ottopress.com/2010/dont-include-wp-load-please Specifically, the "Right way the second" heading. – khromov Sep 1 '14 at 20:52

Your Answer

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

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