tl;dr: 使用调用Contact Form 7时,其快捷码在AJAX请求中不起作用do_shortcode
功能。php
add_action(\'wp_ajax_ps_get_survey_form\', \'ps_get_survey_form\');
add_action(\'wp_ajax_nopriv_ps_get_survey_form\', \'ps_get_survey_form\');
function ps_get_survey_form() {
echo do_shortcode( \'[contact-form-7 id="397" title="Contact form 1"]\' );
die();
}
脚本。js公司
jQuery(document).ready(function($){
$(\'#survey-modal\').fancybox({
href: ajaxurl,
ajax: {
type : "POST",
data : {action: \'ps_get_survey_form\'}
}
});
});
然而,
echo do_shortcode( \'[contact-form-7 id="397" title="Contact form 1"]\' )
返回短码字符串本身。如果我把它放在模板中,效果很好。
尽我所能地尝试(可笑的事情,如将其包装在帖子中/应用\\u内容过滤器、手动触发CF7挂钩的各种操作等),它拒绝将自己转换为表单。
我如何调试它?
EDIT
@Kaiser明智地建议我没有访问do\\u快捷码的权限。我尝试对此进行检查,结果如下:
function ps_get_survey_form() {
//include( ABSPATH . \'wp-includes/shortcodes.php\' );
//Causes fatal error: cannot redeclare add_shortcode
if ( function_exists( \'do_shortcode \' ) ) {
echo "do_shortcode exists";
} else {
echo "do_shortcode doesn\'t exist"; // this gets echoed
}
do_shortcode( \'foo\' ); // this fails silently, even though the function apparently hasn\'t been defined
//foo(); // Causes fatal error as expected
//echo do_shortcode( \'[contact-form-7 id="397" title="contact form 1"]\' );
die();
}