如何让注册用户通过前台改变自己的用户角色?

时间:2016-09-22 作者:JulesB

有时我想让我的注册用户有机会自己免费升级他们的用户角色。我通过编写一个小插件尝试了这一点,但sady我无法在使用短代码时显示html表单,因此无法测试它是否有效。我真的很感谢你的帮助。

function upgrade_to_premium() {
    if( is_user_logged_in() ) {
        if( is_page( \'upgrade-to-premium\' ) ) {
            $current_user = wp_get_current_user();
            if( $current_user->roles[0] == "subscriber" || $current_user->roles[0] == "premium" ) {
                $user_id = $current_user->id;
                $role = $current_user->roles[0];
                if( $_POST[\'role\']){
                    if( $_POST[\'role\'] == $role ) {
                        echo "Sorry, you are already a " . $role . "!";
                    } else {
                        $role = $_POST[\'role\'];
                        $userdata = array();
                        $userdata[\'ID\'] = $user_id;
                        $userdata[\'role\'] = $role;
                        wp_update_user($userdata);
                        echo "Your user type has been changed!  You are now a " . $role . "!";
                    }
                }
                ?>

                    <form method="post" action="">
                        Please select a role:<br/>
                        <select name="role">
                            <option value="subscriber" selected="selected">Subscriber</option>
                            <option value="premium">Premium</option>
                        </select>
                        <INPUT TYPE="submit" name="submit" />
                    </form>

                <?php
            }
        }
    }
}
add_shortcode( \'upgrade_to_premium\', \'upgrade_to_premium\' );

1 个回复
SO网友:Ahmed Fouad

我对代码进行了一些编辑,这可以在我的localhost上运行。试试看,如果可以的话告诉我。

Be careful to edit your own admin role though. 或者设置其他条件,以防止在出现使用错误时更新管理员角色。

Edit: added ob_start() 感谢马克·卡普伦。短代码需要返回而不是回显。

add_shortcode( \'upgrade_to_premium\', \'upgrade_to_premium\' );
function upgrade_to_premium() {

    // Stop if user is not logged in.
    if ( ! is_user_logged_in() )
        return;

    ob_start();

    ?>

    <form method="post" action="">
        Please select a role:<br/>
        <select name="role">
            <option value="subscriber" selected="selected">Subscriber</option>
            <option value="premium">Premium</option>
        </select>
        <input type="submit" name="submit" />
    </form>

    <?php

    // Do not process anything if it\'s not $_POST
    if ( ! isset( $_POST[\'role\'] ) )
        return;

    // Never trust user input.
    $role = sanitize_key( $_POST[\'role\'] );
    if ( ! in_array( $role, array( \'subscriber\', \'premium\' ) ) )
        return;

    // Get the user object
    $user = new WP_User( get_current_user_id() );
    $index = key( $user->roles );
    $user_role = $user->roles[ $index ];

    // User already got that user
    if ( $user_role == $role ) {

        echo sprintf( __( \'You already have %s role.\' ), $role );

    } else {

        // update user role
        $user->set_role( $role );
        echo sprintf( __( \'Your role was changed to %s.\' ), $role );

    }

    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}

相关推荐

Front-end Ajax File Upload

我正在从事一个项目,注册用户可以将文件上传到特定的帖子类型。为了实现更流畅的导航,我使用Ajax制作了整个部分,因此页面永远不会重新加载<然而,我很难<input type=\"file\"> 工作以下是我目前的代码:标记<?php // Start Loop and current meta of the post ?> <form class=\"update_post\" method=\"post\" accept-charset