WordPress.org

Support

Support » Plugins and Hacks » Contact Form 7 » Load content and contact form via ajax, shortcode displayed

Load content and contact form via ajax, shortcode displayed

  • Hey,

    I am trying to display the contact form via ajax with the rest of the content and the result is that the shortcode is displayed when returned by ajax and not form itself.

    If the same page is loaded directly in the browser, the form is displayed.

    I even tested this with a simple blank theme, code below.

    Ajax is called via

    add_action('wp_ajax_load_content', 'th_test_ajax');
    add_action('wp_ajax_nopriv_load_content', 'th_test_ajax');

    Content of the requested page is called via
    new WP_Query(args);

    Here is the full function

    function th_test_ajax()
    {
    	$page_id = $_POST['page_id'];
    
    	$args = array(
    		'page_id' => $page_id,
    		'post_status' => 'publish',
    		'post_type' => array('page')
    	);
    
    	$wp_query = new WP_Query($args);
    
    	if ($wp_query->have_posts())
    	{
    		while ($wp_query->have_posts())
    		{
    			$wp_query->the_post();
    
    			the_content();
    		}
    	}
    
    	die();
    }
    add_action('wp_ajax_load_content', 'th_test_ajax');
    add_action('wp_ajax_nopriv_load_content', 'th_test_ajax');

    Directly access to page:
    The content and the form are shown correctly.

    Via AJAX:
    The content is shown as it should be. Form is NOT shown, instead the shortcode is shown.

    What am I missing here?

    JS test code:

    jQuery(document).ready(function($) {
    	$('body').on('click', '#load_page', function(e){
    		e.preventDefault();
    
    		$.ajax({
    			type: 'POST',
    			url: th.ajax_url,
    			data: { action: 'load_content', page_id: $(this).data('page-id') },
    			cache: false,
    			dataType: 'html'
    		}).done(function(response) {
    			$('#content').html(response);
    		});
    	})
    });

    https://wordpress.org/plugins/contact-form-7/

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Load content and contact form via ajax, shortcode displayed’ is closed to new replies.
Skip to toolbar