wordpress插件add_filter(login_redirect不起作用

时间:2012-08-16 21:23:41

标签: wordpress redirect login wordpress-plugin

我从这个example创建了一个简单的插件:

<?php
/*
Plugin Name: Redirect on login
Plugin URI: some URI
Description: Custom Redirect on login
Author: Some Autor
Version: 1.0
Author URI: some URI
*/

function my_login_redirect($redirect_to, $request){
global $current_user;
get_currentuserinfo();
//is there a user to check?
if(is_array($current_user->roles))
{
    //check for admins
    if(in_array("administrator", $current_user->roles))
       return home_url("/wp-admin/?administrator");
    else
        return home_url("?NOTadministrator");
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);

?>

仅在您已登录时才有效。

请尝试一下 - 将插件代码放入WordPress插件目录 - 激活它 - 确保您已完全注销 - 并且只会将您重定向到/wp-admin/?administrator已登录。

在这种情况下, 10,3 代表什么?

从本网站和其他网站上的其他示例 - 人们正在参考$user->ID而不是全球$user_ID和其他信息 - 但我没有看到任何显示完整的完整代码使用这些方法运行复制粘贴插件。我不是要求某人为我做我的工作 - 我真的需要看到一个完整的例子。

如果你有一个完整的工作示例,清楚地显示了所需的所有变量和值 - 你能在这里发布吗? 非常感谢你。

我正在添加另一个例子,然后跳下一座桥 - 自从我第一次开始编程以来,我没有浪费太多时间。

我不明白 login_redirect 如何工作 - 如果我在调用时无法确定用户信息。

     function my_login_redirect(){


    if($user->ID){

        if($user->has_cap('manage_options')) {
            return home_url("/wp-admin/?administrator");
        }else{
            return home_url("?NOTadministrator");
        }

    }else{

        echo "WHY DOES $user->ID NOT HAVE ANY VALUE YET WHEN CALLING 'login_redirect'? How am I to decide where to send the user - if I do not know who the user is because - I can not get the user ID yet?!";

    }
 }

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

1 个答案:

答案 0 :(得分:2)

  • 10表示您的函数在过滤login_redirect
  • 的过程中所采用的优先级
  • 3表示函数接受的参数数量。

请参阅:http://codex.wordpress.org/Function_Reference/add_filter

相关问题