简体   繁体   English

通过Ajax加载CF7表单

[英]Load CF7 Form via Ajax

i would load via ajax the content of page that have inside the shortcode of one of my form generated via CF7 Plugin. 我会通过ajax加载通过CF7插件生成的我的表单之一的简码内的页面内容。 When the content is rendered the shortcode is not processed and come print as text. 呈现内容时,不会处理短代码,而是将其打印为文本。 Is there anyway to force execution of shortcode in an ajax call? 无论如何,是否有必要在ajax调用中强制执行短代码? Thank you, M. 谢谢你

This is the js script: 这是js脚本:

function getcontent(postid){
    // here is where the request will happen
    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php',//ajax controller request
        data:{
            'action':'dch',//action invoked
            'fn':'ghcontent',//function required
            'postid':postid//if of post required
        },
        cache: false,
        async:true,
        timeout: 3000,
        success:function(data){
            jQuery("#posthome").html(data);//print the html (content)
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
}

And this is my php code: 这是我的php代码:

add_action('wp_ajax_nopriv_dch', 'ghcontent');
add_action('wp_ajax_dch', 'ghcontent');
function ghcontent(){
  $args = array('page_id' => $_REQUEST['postid']);
  query_posts($args);
  if(have_posts()) : 
    while (have_posts()) : the_post();
      the_content();
    endwhile;
  endif;
  die();
}

Shortcodes are applied when the the_content filter runs: the_content过滤器运行时,将应用简码:

$post = get_post( $_REQUEST['postid'] );
$return = apply_filters( 'the_content', $post->post_content );
echo $return;
die();

do_shortcode won't work in your admin-ajax.php Instead try generating your own ajax action. do_shortcode在您的admin-ajax.php中不起作用,而是尝试生成自己的ajax操作。 Check the following answer to a similar question: https://wordpress.stackexchange.com/questions/53309/why-might-a-plugins-do-shortcode-not-work-in-an-ajax-request 检查对类似问题的以下答案: https : //wordpress.stackexchange.com/questions/53309/why-might-a-plugins-do-shortcode-not-work-in-an-ajax-request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM