【问题标题】:Always redirect if user has specific role如果用户具有特定角色,则始终重定向
【发布时间】:2017-06-27 11:37:44
【问题描述】:

我有具有角色的用户。在用户登录期间,我想检查角色是否为“gdmp_subscriber”以及是否为真-> 重定向到特定于 url 的 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(); ?>

【问题讨论】:

    标签: php wordpress function redirect


    【解决方案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 订阅者”用户角色的任何用户重定向到您选择的网址。

    【讨论】:

      猜你喜欢
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      相关资源
      最近更新 更多