3

WordPress版本4.7.5使用wp_set_auth_cookie有任何挂钩

我从Instagram的注册新用户,我能成功注册新用户。注册后,我正在尝试登录用户。

我可以使用钩子,但想比较好的解决方案,我 已张贴,因为这将是完全无用的。

if($user_created) { 
    $new_user = get_user_by('id', $user_created); 
    //$loggedin = programmatic_login($new_user->user_login); 
    //ob_start(); 
    if (!is_user_logged_in()) { 
     wp_clear_auth_cookie(); 
     wp_set_current_user($user_created, $new_user->user_login); 
     wp_set_auth_cookie($user_created, true); 
     do_action('wp_login', $new_user->user_login); 
     // wp_safe_redirect(site_url() . '/profile/'); 
     // exit; 
     // $redirect_to=user_admin_url(); 
     // wp_safe_redirect($redirect_to); 
     // exit(); 
    } 
    //ob_end_clean(); 
} ?> 
<script>jQuery(document).ready(function(){ window.location.href = "<?php echo site_url() . '/profile/'; ?>";});</script> <?php 

也试图从另一篇文章的自定义programmatic_login功能。

,如果我试图var_dumpwp_get_current_user()$_COOKIE低于此,我获取用户对象wp_get_current_user()array(1) { ["wordpress_test_cookie"]=> string(15) "WP Cookie check" }$_COOKIE

重要编辑

的代码是一个函数,它挂到钩子,即在一侧称为一个网页,是越来越显示头之后里面,所以我们不能在这里使用的init。任何其他的做法,也是因为这个wp_safe_redirect()header('Location: ')也无法正常工作。

重要更新与我刚才想

一个肮脏的工作,各地

当我创建从Instagram的注册用户,成功创建后,我重定向用户到一个页面与GET参数像?user_id=$inserted_id和WP挂钩,我试图得到GET['user_id']并记录它,它的工作,试图找到一个合适的解决方案。

if ($wpdb->insert_id){ //that is registration with instagram 
    ?> 
    <script>jQuery(document).ready(function(){ window.location.href = "<?php echo get_bloginfo('url') . '/profile/?id=' . $wpdb->insert_id; ?>";});</script> 
    <?php 
} 

然后

add_action('wp', 'login_current_user'); 
function login_current_user(){ 
    if (is_page() && get_the_id() == 863){ 
     if (isset($_GET['id'])){ 
      if (!is_user_logged_in()) { 
       $user_id = $_GET['id']; 
       $user = get_user_by('id', $user_id); 
       wp_clear_auth_cookie(); 
       wp_set_current_user($user_id, $user->user_login); 
       wp_set_auth_cookie($user_id, true); 
       do_action('wp_login', $user->user_login); 
       if (is_user_logged_in()){ 
        $redirect_to=site_url() . '/profile'; 
        wp_safe_redirect($redirect_to); 
        exit(); 
       } 
      } 
     } 
    } 
} 

与使坏Proble。如果我们传递的id作为获取参数 存在于wp_users表中,那么将不会验证将会记录在 中。

更新

我创建了一个user_meta重定向到的个人资料页验证登录用户之前和之后,除去usermeta,就像一个OTP。尽可能让它变得更好。

代码如下: -

if ($wpdb->insert_id){ //that is registration with instagram 
    $temporary_token = sha1(rand()); 
    update_user_meta($wpdb->insert_id, 'temporary_token', $temporary_token); 
    ?> 
    <script>jQuery(document).ready(function(){ window.location.href = "<?php echo get_bloginfo('url') . '/profile/?id=' . $wpdb->insert_id . '&token=' . $temporary_token; ?>";});</script> 
    <?php 
} 

然后

add_action('wp', 'login_current_user'); 
    function login_current_user(){ 
     if (is_page() && get_the_id() == 863){ 
      if (isset($_GET['id'])){ 
       if (!is_user_logged_in()) { 
        $user_id = $_GET['id']; 
        $user = get_user_by('id', $user_id); 
        if ($_GET['token'] == get_user_meta($user_id, 'temporary_token', true)){ 
         delete_user_meta($user_id, 'temporary_token', $_GET['token']); 
         wp_clear_auth_cookie(); 
         wp_set_current_user($user_id, $user->user_login); 
         wp_set_auth_cookie($user_id, true); 
         do_action('wp_login', $user->user_login); 
         if (is_user_logged_in()){ 
          $redirect_to=site_url() . '/profile'; 
          wp_safe_redirect($redirect_to); 
          exit(); 
         } 
        } 
       } 
      } 
     } 
    } 
+0

如果头文件在'wp_set_auth_cookie()'之前发送,那么cookie可能不会被设置。如果你可以挂钩init动作add_action('init','my_action');'并且在那里打电话,它可能会解决这个问题。 – Brian

回答

3

如果调用之前wp_clear_auth_cookie()wp_set_auth_cookie()然后既不将工作都依赖于PHP的setcookie函数头部已被发送。after_setup_theme行动将发生在标题发送之前,因此挂钩并在那里设置cookie应该可以解决问题。

function myPlugin_createUser(){ 
    // Probably need to put code that creates user here too otherwise $user_created will never return true 
    if($user_created) { 
     $new_user = get_user_by('id', $user_created); 
     if (!is_user_logged_in()) { 
      wp_clear_auth_cookie(); 
      wp_set_current_user($user_created, $new_user->user_login); 
      wp_set_auth_cookie($user_created, true); 
      do_action('wp_login', $new_user->user_login); 
     } 
    } 
} 

add_action('after_setup_theme', 'myPlugin_createUser'); 

更新:

如果您输出视图元素触发您createUser函数之前,它不会工作,所以我们需要确保它们运行完全相互独立的。在下面,我已经汇总了一个简单的例子来说明如何实现这一点 - 在视图中我们输出一个传递GET参数的链接,这个参数被插件拾取并且在渲染任何东西之前运行动作,以允许我们设置cookie 。

我-plugin.php

// Note: you'll need to include a completed version of myPlugin_createUser() function as above for these examples to work 
add_shortcode('my-plugin', function() { 
    // I've used a GET request for simplicity- a form + POST request may be more practical for your purposes and will prevent caching issues. 
    echo "<a href='?my_plugin_login_user=1'>Login</a>"; 
}); 

// Check for our GET parameter 
if(isset($_GET['my_plugin_login_user'])) { 
    // Nothing should be rendered yet as plugin template hasn't even started loading yet so cookies should set fine 
    add_action('plugins_loaded', 'myPlugin_createUser'); 
} 

页-MY-的template.php

<div> ... 
    <?php 
     do_shortcode('my-plugin'); 
    ?> 
</div> 

更新2: 如果使用Instagrams验证API明确流动概述https://www.instagram.com/developer/authentication/ (大多数下面使用的例子都是从那里取得的)可能是最好的方法。下面我将简要介绍一下你如何实现它。其中大部分是从那里添加的笔记。

发送用户关闭以https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

你可以包括WordPress的用户ID作为回调URL,或者如果用户尚未创建参数,你需要保持状态,那么你会需要生成一个临时令牌,稍后您可以使用该临时令牌与数据相关联,或者如果数据客户端不敏感,可能更容易,更好,因为它不需要用户登录即可运行。

用户将请登录

不多说了关于这一步 - 如果出现错误,他们会被重定向到http://your-redirect-uri?error=access_denied&error_reason=user_denied&error_description=The+user+denied+your+request

如果登录成功,用户将被重定向到http://your-redirect-uri?code=CODE

CODE传回我们可以兑换访问令牌的重定向网址。

所以从这里我们可以处理它的方式有很多,但实质上我们需要的是重定向的端点,我们有足够的控制权来发送任何HTTP响应主体之前的HTTP头。接近这一(不是一个详尽的列表)的

方法:

  • 有条件页面上AUTHS的Instagram允许挂钩(如你的例子)。这具有不需要额外重定向的优点。
  • 自定义页面模板
  • 外部是手动自举WordPress的脚本做什么,我们需要的,然后在

重定向回所以现在一旦我们得到了一个端点设置,我们需要赎回CODE的访问令牌。从https://gist.github.com/arturmamedov/7f5a90b85a20e06e344ebb88dc898d25

$uri = 'https://api.instagram.com/oauth/access_token'; 
$data = [ 
    'client_id' => '213esdaedasdasd12...YOUR_CLIENT_ID', 
    'client_secret' => 'a8b4aaf06c0da310...YOUR_CLIENT_SECRET', 
    'grant_type' => 'authorization_code', 
    'redirect_uri' => 'http://www.YOUR_REDIRECT_URL.it',  // The exact redirect URL as used previously 
    'code' => $_GET['code'] 
]; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $uri); // uri 
curl_setopt($ch, CURLOPT_POST, true); // POST 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // POST DATA 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN RESULT true 
curl_setopt($ch, CURLOPT_HEADER, 0); // RETURN HEADER false 
curl_setopt($ch, CURLOPT_NOBODY, 0); // NO RETURN BODY false/we need the body to return 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // VERIFY SSL HOST false 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // VERIFY SSL PEER false 
$result = json_decode(curl_exec($ch)); 

$结果适应这个片段将是JSON响应的对象表示:

{ 
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d", 
"user": { 
    "id": "1574083", 
    "username": "snoopdogg", 
    "full_name": "Snoop Dogg", 
    "profile_picture": "..." 
    } 
} 

从这里我们只是创造我们的用户/与WordPress验证和重定位到所需页/渲染模板(如果通过条件钩子处理,则不需要重定向),并在需要时恢复页面状态。

+0

感谢您的回应,似乎我不能在这里使用init钩子。在我的搜索过程中,我发现有些人已经提出了它,但在我的情况下,我无法使用,我甚至发现即使我不想重定向,我可以使用ob_start()和ob_end(),但那也没有奏效。 –

+1

非常欢迎:)对不起,你不能使用'init'?或者'init'不起作用?如果既没有输出缓冲,也没有'init'动作,那么很可能出现了一些输出或者对'add_action'的调用发生得太晚了,所以为了解决这个问题,我们可能需要更多地了解这个被调用的方式和位置如果'$ user_created'的计算结果为'false',则cookie可能不会被设置的另一个原因是一个更大的代码片段。 – Brian

+0

感谢您为添加更多创意而付出的努力,$ user_created和wp_get_current_user()正常工作,正如我所描述的那样。如果我可以在这里使用do操作并更新问题,我正在重新调查。 –