如何创建和检索特殊注册表中的数据?

时间:2012-02-25 作者:Daniel

解决以下问题的建议方法是什么?我必须创建两个页面。在一个页面上,我将有一个小表单,用户可以在其中注册自己(有照片或没有照片)。在另一个页面上,我将显示在上一个表单上输入的所有用户信息。对于管理部分,我只需要列出所有注册用户,并能够删除他们。

我不想碰默认的WP用户注册的东西,因为它已经被使用了,我不能混淆这些东西。

是否可以使用某种自定义帖子类型?有插件或想法吗?

2 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

以下代码创建一个新角色。事实上,它只是克隆订阅者角色,所以如果您当前没有使用该角色,那么您也可以使用它。

以下函数只需运行一次(当用户是“管理端”时运行)

//Creates a custom role called \'my_new_role\'.
//Run this once then feel free to comment out the next line:
add_action(\'admin_init\', \'my_custom_role\');
function my_custom_role(){
    global $wp_roles;

    $subscriber = $wp_roles->get_role(\'subscriber\');

    //Adding a \'new_role\' with subscriber caps
    $wp_roles->add_role(\'my_new_role\', \'My New Role\', $subscriber->capabilities);
    //Optional add/remove caps, like the capability to access the dashboard
    //$wp_roles->remove_cap(\'my_new_role\',\'read\');  
}
参见法典more information on capabilities.

无论您在哪里处理表单,都需要创建一个新用户,并为他们分配角色“my\\u new\\u role”。要执行此操作,请使用wp_insert_user. 举个简单的例子:

wp_insert_user( array (
    \'user_login\' => \'JoeBloggs\',
    \'user_pass\' => \'a_password_43463\', 
    \'first_name\' => \'Joseph\',
    \'last_name\' => \'Bloggs\',
    \'role\'=>\'my_new_role\') ) ;
Prior to the above you should have performed all the nonce-checks, data validation and any other checks.

您可能希望在这些新用户登录时将其重定向到其他页面(而不是仪表板)。要执行此操作,请使用login_redirect 滤器

add_filter(\'login_redirect\', \'dashboard_redirect\');
function dashboard_redirect($url) {
    global $current_user;
    get_currentuserinfo();

    if (current_user_can(\'my_new_role\')) {
             //current user is \'my_new_role\', take them somewhere else...
             $url = home_url(); 
        }
        return $url;
    }

SO网友:andresmijares

我会这样面对:

安装Contact Form 7Database extension

结束