简体   繁体   中英

Always redirect if user has specific role

I have users with roles. During the login of user, i want to check if the role is "gdmp_subscriber" and if it's true -> redirect to url specific url. If not just login. I've tried the following things, but withour success. Any ideas ?

Class:

<?php
define( 'GDMP_VERSION', '1.0' );
define( 'GDMP_PATH', dirname( __FILE__ ) );
define( 'GDMP_PATH_INCLUDES', dirname( __FILE__ ) . '/inc' );
define( 'GDMP_FOLDER', basename( MP_PATH ) );
define( 'GDMP_URL', plugins_url() . '/' . MP_FOLDER );
define( 'GDMP_URL_INCLUDES', MP_URL . '/inc' );


class GD_Marketing_Portal {

    public function __construct() {
        register_activation_hook( __FILE__, 'gdmp_on_activate_callback' );
        register_deactivation_hook( __FILE__, 'gdmp_on_deactivate_callback' );
        add_action('wp_login', array($this, 'redirect_to_mp'), 10, 2);
    }
    public function redirect_to_mp($user) {
        if (user_can($user, 'gdmp_subscriber')) {
            wp_redirect(home_url('marketing-portal'));
        }
    }
}
function gdmp_on_activate_callback() {
    $capabilities = array(
        'read' => true,
    );

    add_role( 'gdmp_subscriber', __( 'Marketing Portal Subscriber', 'gdmp' ), $capabilities );
}

function gdmp_on_deactivate_callback() {
    if( get_role( 'gdmp_subscriber' ) ) {
      remove_role( 'gdmp_subscriber' );
    }
}
$marketing_portal = new GD_Marketing_Portal();

And the page html :

get_header(); ?>
<?php
if ( is_user_logged_in() ) {
?>
<section class="section">
    <div class="row">
        <?php
        if ( ! post_password_required() ) {
        ?>
        <div class="column thumbnail-wrapper">
            <?php the_post_thumbnail( 'full' ); ?>
        </div><!-- .column -->

        <div class="column title-wrapper">
            <h1><?php the_title(); ?></h1><!-- .heading-title -->
        </div><!-- .column -->

        <div class="column  subtitle-wrapper">
            <h4><?php echo $subtitle ?></h4><!-- .heading-title -->
        </div><!-- .column -->
    </div><!-- .row -->
    <?php
    }
    ?>
    <div class="row">
        <div class="column">
            <?php the_content(); ?>
        </div>
    </div><!-- .row -->
    <?php
    if ( ! post_password_required() ) {
            ?>
            <?php if ( $form_1_shortcode || $form_2_shortcode ) : ?>
                <div class="row">
                    <div class="column" id="mp-form-tabs">
                        <ul>
                            <?php if ( $form_1_shortcode ) : ?>
                                <li><a href="#mp-form-1">Form 1</a></li>
                            <?php endif;
                            if ( $form_2_shortcode ) : ?>
                                <li><a href="#mp-form-2">Form 2</a></li>
                            <?php endif; ?>
                        </ul>
                        <?php if ( $form_1_shortcode ) : ?>
                            <div id="mp-form-1">
                                <p><?php echo do_shortcode( $form_1_shortcode ); ?></p>
                            </div>
                        <?php endif;
                        if ( $form_2_shortcode ) : ?>
                            <div id="mp-form-2">
                                <p><?php echo do_shortcode( $form_2_shortcode ); ?></p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>
            <?php
            }
        ?>
    </section>
    <?php
} else {
    auth_redirect();
}
?>
<?php get_footer(); ?>
function redirect_users_by_role() {

    $current_user   = wp_get_current_user();
    $role_name      = $current_user->roles[0];

    if ( 'gdmp_subscriber' === $role_name ) {
        wp_redirect( get_home_url() . '/path' );
        exit;
    }

}
add_action( 'admin_init', 'redirect_users_by_role' );

Add the following to your functions.php. It will redirect any users with the user role of 'gdmp subscriber' to your chosen url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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