admin管理员组

文章数量:1289404

I'm trying to give guest users "not logged in" a role.

I searched everywhere and I could not find a single solution that can make that happen. All what I found is to make an if statement in the functions.php file to give access to certain things, like for example post a comment without logging in.

However when there are a lot of roles it's hard to make it that way and it starts to be complicated.

Is there any way that I can achieve that?

Things that I have used

add_role( 'custom_role', 'Custom Role', array( 'read' => true ) );

and

 <?php
 global $user_login;
 if( $user_login ) {
    echo 'user logged in';
 } else {
    echo 'user not logged in';
 }
 ?>

I'm trying to give guest users "not logged in" a role.

I searched everywhere and I could not find a single solution that can make that happen. All what I found is to make an if statement in the functions.php file to give access to certain things, like for example post a comment without logging in.

However when there are a lot of roles it's hard to make it that way and it starts to be complicated.

Is there any way that I can achieve that?

Things that I have used

add_role( 'custom_role', 'Custom Role', array( 'read' => true ) );

and

 <?php
 global $user_login;
 if( $user_login ) {
    echo 'user logged in';
 } else {
    echo 'user not logged in';
 }
 ?>
Share Improve this question edited May 17, 2020 at 0:41 fuxia 107k38 gold badges255 silver badges459 bronze badges asked May 16, 2020 at 19:04 N mN m 33 bronze badges 2
  • So you want to rename the guest role? Or do you want to change the permissions the guest role has? Why do you want the guests to have another role? You could adjust the permissions for the existing role or just rename it to your needs. – rank Commented May 16, 2020 at 23:16
  • 1 I want to show somethıng to users who doesnt logın yet to my websıte – N m Commented May 17, 2020 at 11:43
Add a comment  | 

2 Answers 2

Reset to default 0

You need to add to the template directly or via a short code from PHP Snippets plugin.

Source: https://developer.wordpress/reference/functions/is_user_logged_in/

   <?php
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}
?>

If you only want to show content to logged in users and not logged in users, you don't need to create new role for this. Wordpress will notice if a user is logged in, if you use the following function:

<?php
    if ( is_user_logged_in() ) {
        echo 'Welcome, registered user!';
    } else {
        echo 'Welcome, visitor!';
    }
?>

https://developer.wordpress/reference/functions/is_user_logged_in/

So like you wrote in the comment, you want to show text if the user is not logged in. You can write in the template file of your theme (where you want the message to show, check out: https://developer.wordpress/themes/basics/template-hierarchy/) an if statement. If the is_user_logged_in is not (using !) true, show the message:

<?php
    if ( !is_user_logged_in() ) {
        echo "You are not logged in.";
    }
?>

本文标签: How to give guest users quotnot logged inquot a role