WordPress.org

Forums

Contact Form 7
Load content and contact form via ajax, shortcode displayed (3 posts)

  1. lessthanweb
    Member
    Posted 1 year ago #

    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/

  2. Sebastian
    Member
    Posted 1 year ago #

    I have same problem, but no solutions yet.

  3. esmi
    Forum Moderator
    Posted 1 year ago #

    @Sebastian: If you require assistance then, as per the Forum Welcome, please post your own topic instead of tagging onto someone else's topic. This topic refers to an old version of WordPress.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Contact Form 7
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic