2016-03-24 15 views
1

私はWordPressに登録していますが、ログイン後にユーザーをリダイレクトしようとしていますが、特定のページからサインインした場合のみ(スラッグは「アップグレード - 今すぐゴールドクラスのコーチング」)ユーザーが特定のページからログインした場合のみ、ログイン後にリダイレクトするにはどうすればよいですか?

ここ私がこれまでに作ったものです:

function my_login_redirect($redirect_to, $request, $user) { 
    //did they try to login from this page? 
    if ($request == "upgrade-now-gold-class-coaching") { 
     //is there a user to check? 
     if (isset($user->roles) && is_array($user->roles)) { 
      //check for admins 
      if (in_array('administrator', $user->roles)) { 
       // redirect them to the default place 
       return $redirect_to; 
      } else { 
       return "https://www.drewbairdfitness.com/11674-2/"; 
      } 
     } else { 
      return $redirect_to; 
     } 
    } else { 
     return $redirect_to; 
    } 
} 

add_filter('login_redirect', 'my_login_redirect', 10, 3); 

ありがとう!

+0

あなたのログインフォームはどこにありますか?あなたの特定のページにありますか、それともデフォルトのログインフォーム(wp-admin/wp-login.php)を使用していますか? – Jevuska

+0

これは別のページにあります。実際には/ upgrade-now-gold-class-coaching/'にあります - 私は複数のログインページを持つ予定です。ログイン後に各自が別の場所にリダイレクトされるようにする –

答えて

0

@ジャッジドコーディング、火災login_redirect早くリダイレ​​クトするには、wp_safe_redirectを使用できます。ここでは、特定のページから別のページへのログインを要求し、ユーザーの役割がadministratorではない場合、アプローチリダイレクトの例を示します。

そのURLを使用ページにリダイレクト:

add_filter('login_redirect', 'my_login_redirect', 10, 3); 

function my_login_redirect($redirect_to, $request, $user) 
{ 
    /* target certain page with their part url */ 
    if (false !== strpos($request, 'upgrade-now-gold-class-coaching') 
     && ! empty($user->roles)) 
    { 
     /* exclude administrator as role 
     * 
     */ 
     if (is_array($user->roles) 
      && ! in_array('administrator', $user->roles)) 
     { 
      /* redirect to another url 
      * you can use function get_page_link(40), mean page id 40 
      */ 
      $redirect_to = get_page_link(40); 
      wp_safe_redirect($redirect_to, 302); 
      exit; 
     } 
    } 
    return $redirect_to; 
} 

:そのIDを使用してページに

add_filter('login_redirect', 'my_login_redirect', 10, 3); 

function my_login_redirect($redirect_to, $request, $user) 
{ 
    /* target certain page with their part url 
    * you can use url_to_postid($request) 
    * and pass in conditional statement with your target post id 
    * target post/page id = 7 
    * if (7 == url_to_postid($request)) 
    * make it sure you have hidden input name redirect_to 
    */ 
    if (false !== strpos($request, 'upgrade-now-gold-class-coaching') 
     && ! empty($user->roles)) 
    { 
     /* exclude administrator as role 
     * 
     */ 
     if (is_array($user->roles) 
      && ! in_array('administrator', $user->roles)) 
     { 
      /* redirect to another url 
      * we use hardcode slug 
      */ 
      $redirect_to = home_url('/11674-2/'); 
      wp_safe_redirect($redirect_to, 302); 
      exit; 
     } 
    } 
    return $redirect_to; 
} 

リダイレクトを火災フィルタ機能login_redirectすることで、あなたは、カスタム・ログインを使用フォームの場合は、デフォルトのwpログインフォームなどの入力名redirect_toが非表示になっていることを確認してください。wp-login.php

関連する問題