使用 JavaScript/jQuery 调用 do_action

Yoh*_*hio 5 javascript php ajax wordpress jquery

userpro_ajax_url我有一个 AJAX 函数,可以在通过 Facebook 成功登录后发送信息。

do_action我正在尝试使用以下方法获得成功块来运行函数

<?php 
ob_start();  
do_action('userpro_social_login', <email needs to go here>);
ob_clean();
?>
Run Code Online (Sandbox Code Playgroud)

现在,如果我手动传递电子邮件地址,它可以正常工作,但是我可以动态获取电子邮件的唯一方法是通过 JavaScript 中的当前响应。

完整的功能是:

FB.api('/me?fields=name,email,first_name,last_name,gender', function(response) {
    jQuery.ajax({
        url: userpro_ajax_url,
        data: "action=userpro_fbconnect&id="+response.id+"&username="+response.username+"&first_name="+response.first_name+"&last_name="+response.last_name+"&gender="+response.gender+"&email="+response.email+"&name="+response.name+"&link="+response.link+"&profilepicture="+encodeURIComponent(profilepicture)+"&redirect="+redirect,
        dataType: 'JSON',
        type: 'POST',
        success:function(data){
            userpro_end_load( form );
            <?php 
            ob_start();  
            do_action('userpro_social_login', );
            ob_clean();
            ?>
            /* custom message */
            if (data.custom_message){
                form.parents('.userpro').find('.userpro-body').prepend( data.custom_message );
            }
            /* redirect after form */
            if (data.redirect_uri){
                if (data.redirect_uri =='refresh') {
                    //document.location.href=jQuery(location).attr('href');
                } else {
                    //document.location.href=data.redirect_uri;
                }
            }
        },
        error: function(){
            alert('Something wrong happened.');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下命令运行 php 操作:

jQuery(document).trigger('userpro_social_login', response.email);
Run Code Online (Sandbox Code Playgroud)

这些都不起作用...我做错了什么?

mad*_*scu 1

修改您的 php 函数userpro_fbconnect触发那里的代码或在完成此请求后创建另一个 ajax 请求