WordPress: How To Check If The User is Logged, Determine Their Role, And Modify Content

We have a lot of authors on Martech Zone but not all of them are familiar with WordPress and how to log in and publish on the site. WordPress can identify whether or not you’re logged into the platform using a user_level function that returns whether or not you’re logged in as well as what your role is.

Check User Role for Logged In WordPress User

WordPress has several default roles and capabilities:

You can dynamically change the displayed link on your site using this snippet using WordPress’ API… displaying the admin link for an author, but displaying an advertise link for anyone that isn’t logged in:

$user = wp_get_current_user();

if ( in_array( 'author', (array) $user->roles ) ) {
    wp_register('<li class="menuitem">','</li>');
} else {
    echo "<li class='menuitem'><a href='https://martech.zone/advertise/' title='Advertise'>Advertise</a></li>";
}

?>
Exit mobile version