0

I made a plugin in wordpress for a form and ajax. When the page successful load the content via ajax, I want to use contact form 7 on the page. I use the do_shortcode() tag as explain on contact form 7 faqs but it's fail to display the form. Below are the example code that I am doing:

function.php

add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' );

function ajax_test_enqueue_scripts() {

    wp_enqueue_script( 'season', plugins_url( '/form-submit.js', __FILE__ ), array('jquery'), '1.0', true );

    wp_localize_script( 'season', 'myAjax', array(
        'ajax_url' => admin_url( 'admin-ajax.php' )
    ));

}

add_action('wp_ajax_confirmRequest', '_myConfirmHandler');
add_action('wp_ajax_nopriv_confirmRequest', '_myConfirmHandler');

function _myConfirmHandler()
{
    if(isset($_POST['vehicle_no']))
    {
        $vehicle_no = $_POST['vehicle_no'];
        $email = $_POST['email'];

        echo "Your Data: <br>$vehicle_no <br>$email<p />";
        echo do_shortcode('[cfdb-table form="season parking form_copy" filter="your-platno=' . $vehicle_no . '"]');
        echo do_shortcode('[contact-form-7 id="6871" title="season parking form_copy"]'); 
    }

    exit;
}

jQuery

jQuery(document).ready(function($) {
    $('#submit_payment').click(function(e){
        e.preventDefault();
        var str = $("form[name=season-form]").serialize();
        $.ajax({
            type: "POST",
            url: myAjax.ajax_url,
            data: str + '&action=confirmRequest' 
        }).done(function(data){
            $("#result").html(data);
        });
    });
});
  1. It will just output [contact-form-7 id="6871" title="season parking form_copy"]
  2. I also use the Contact Form DB but that display the data without a problem.
  3. Do I need to enqueue the script for contact form 7 as well. If need, how to do it?
1

It is hard to give a 100% answer without seeing the code of how the shortcode is being registered, but from your description it sounds like it is not being registered when ajax requests are being processed. The reason is probably because ajax requests are being handled in an "admin" conttext, and shortcodes are a front end artifact and therefor the plugin author might decide to not register at all when on admin side.

there are several possible solutions

  • Do not (and try never to) use do_shortcode just call the api that generates the shortcode directly
  • Register the shortcode yourself.
  • Use the wp-json endpoint with register_rest_route instead of the the admin-ajax.php one. It do not assume admin context and it is somewhat faster as it do not do admin related initialization.
  • thank you for suggesting another method, meanwhile updated shared code above is working perfectly fine on my end. As, it will run only after website is fully loaded. So, please can you clarify this statement of yours: It is hard to give a 100% answer without seeing the code of how the shortcode is being registered, but from your description it sounds like it is not being registered when ajax requests are being processed. I want to know reason behind this. – BlueSuiter Apr 29 '17 at 6:00
  • 1
    The reason is that the ajax endpoint which was introduced at about version 2.5 IIRC was used then only for admin ajax, and only later it was extended to support non admin use. Due to that legacy the ADMIN define is set before anything is being done, and therefor is_admin() will return true. – Mark Kaplun Apr 29 '17 at 6:24

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.