我试图使用Ultimate Facebook插件的一个短代码在AJAX弹出窗口中显示FB connect按钮,但似乎无法正常工作。我搜索并使用了djb在这个问题上给出的解决方案-Why might a plugin's 'do_shortcode' not work in an AJAX request?, 但它只起了部分作用。理想情况下,我希望用户能够使用FB登录,然后将列表保存到她的帐户。我想使用插件而不是嵌入直接代码,因为当有人使用FB connect时,也会在wordpress中创建新用户。
复制问题的程序-
转到http://www.ajaymreddy.com/stg/country/india/#单击开始选择任何复选框-注意不要在复选框旁边单击文本如果您没有登录,则应显示fb connect按钮。但是,当前仅显示短代码文本Code in functions.php -
//From https://wordpress.stackexchange.com/questions/53309/why-might-a-plugins-do-shortcode-not-work-in-an-ajax-request
add_action( \'init\', function() {
ps_register_shortcode_ajax( \'show_fblogin\', \'show_fblogin\' );
} );
function ps_register_shortcode_ajax( $callable, $action ) {
if ( empty( $_POST[\'action\'] ) || $_POST[\'action\'] != $action )
return;
call_user_func( $callable );
}
function show_fblogin(){
if (!is_user_logged_in())
echo do_shortcode(\'[wdfb_connect]\');
die ();
}
add_action( \'wp_ajax_show_fblogin\', \'show_fblogin\' );
add_action( \'wp_ajax_nopriv_show_fblogin\', \'show_fblogin\' );
Code in ajax file -
$(document).on( \'click\', \'#saveBtn\', function (){
if (myAjax.loggedin==1)
{
jQuery.ajax({
type: \'POST\',
url: myAjax.ajaxurl,
data: {
action: \'save_whslist\',
selected: selected.toString(),
whsNonce: myAjax.selectNonce,
},
success: function(data, textStatus, XMLHttpRequest){
selected = [];
modal.close();
$("input.whsites").prop(\'disabled\', true);
$("input.whsites").prop(\'checked\', false);
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown+\' fail\');
}
});
}
else
{
jQuery.ajax({
type: \'POST\',
url: myAjax.ajaxurl,
data: {
action: \'show_fblogin\',
selected: selected.toString(),
whsNonce: myAjax.selectNonce,
},
success: function(data, textStatus, XMLHttpRequest){
modal.open({content: "<p>Please login to save your travel list<br /><span>No auto posts on your wall!</span></p>"+data});
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown+\' fail\');
}
});
}
});