繁体   English   中英

wordpress插件add_filter(login_redirect不起作用

[英]wordpress plugin add_filter(login_redirect does not work

我从这个例子中创建了一个简单的插件:

<?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);
  • 10表示您的函数在过滤login_redirect的其他函数中所采用的优先级
  • 3表示函数接受的参数数量。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM