我有角色的用户。在用户登录期间,我想检查角色是否是" gdmp_subscriber"如果它是真的 - >重定向到url特定网址。如果不只是登录。我已经尝试过以下的事情,但是我们已经成功了。有什么想法吗?
类别:
<?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();
页面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(); ?>
答案 0 :(得分:1)
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' );
将以下内容添加到functions.php中。它会将具有“gdmp subscriber”用户角色的任何用户重定向到您选择的URL。