forms - How to Create a login for for subscribers only

admin2025-01-08  6

does anyone tried creating a login form specifically for Subscribers? meaning if the user roles are admins, contributors, authors are not able to login on that said form only subscriber users. Thanks!

does anyone tried creating a login form specifically for Subscribers? meaning if the user roles are admins, contributors, authors are not able to login on that said form only subscriber users. Thanks!

Share Improve this question asked Oct 17, 2016 at 7:59 SayrooseSayroose 311 silver badge3 bronze badges 3
  • By login do you mean access to the dashboard, just plain login in. If its plain login you want, you could try to check the user role of the user who is trying to log in, if the user is not a subscriber, send them a message stating that they are not able to login and destroy their session – bagpipper Commented Oct 17, 2016 at 10:13
  • i think that's a good idea. do you have some code snippet on checking the user role after logging in? – Sayroose Commented Oct 17, 2016 at 10:28
  • Possible duplicate of How do I create a specific login page for a specific user? – Andy Macaulay-Brook Commented Oct 17, 2016 at 10:44
Add a comment  | 

2 Answers 2

Reset to default 0

Here is a snipped. Make sure to run this code after the users are logged in

//To get logged in user information
$user = wp_get_current_user();
//Condition to check weather user is subscriber or not
if ( in_array( 'subscriber', (array) $user->roles ) ) {
    //The user has the "subscriber" role
}else{
 // redirect user to different section and end their session
 wp_logout(); // user is logged out
 wp_redirect( home_url( '/' ) ); // user is redirected to home page
}

Do let me know if this works for you or not

You can create a custom login form that restricts access to subscribers only by checking the user role during the login process.

Create a new page template for your custom login page

Add the following code to custom-login.php

<?php
/**
 * Template Name: Custom Login
 */

// Check if user is already logged in, if yes, redirect to home page
if (is_user_logged_in()) {
    wp_redirect(home_url());
    exit;
}

// Check if form is submitted
if (isset($_POST['submit'])) {
    $username = sanitize_text_field($_POST['username']);
    $password = $_POST['password'];

    // Attempt login
    $user = wp_signon(array(
        'user_login'    => $username,
        'user_password' => $password,
        'remember'      => true,
    ));

    // Check if login successful
    if (is_wp_error($user)) {
        $error_message = $user->get_error_message();
    } else {
        // Redirect user after successful login
        wp_redirect(home_url());
        exit;
    }
}
?>

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <title><?php bloginfo('name'); ?> - Custom Login</title>
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>

<div id="login-form">
    <h2>Login</h2>
    <?php if (isset($error_message)) : ?>
        <p class="error"><?php echo $error_message; ?></p>
    <?php endif; ?>
    <form method="post" action="">
        <p>
            <label for="username">Username</label>
            <input type="text" name="username" id="username" required>
        </p>
        <p>
            <label for="password">Password</label>
            <input type="password" name="password" id="password" required>
        </p>
        <p>
            <input type="submit" name="submit" value="Login">
        </p>
    </form>
</div>

<?php wp_footer(); ?>
</body>
</html>

After that create a new page in Wordpress admin and assign the template Custom Login to it.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268557a1284.html

最新回复(0)