Content Marketing

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:

  • Super Admin – somebody with access to the site network administration features and all other features.
  • Administrator – somebody who has access to all the administration features within a single site.
  • Editor – somebody who can publish and manage posts including the posts of other users.
  • Author  – somebody who can publish and manage their own posts.
  • Contributor – somebody who can write and manage their own posts but cannot publish them.
  • Subscriber – somebody who can only manage their profile.

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>";
}

?>

Douglas Karr

Douglas Karr is a fractional Chief Marketing Officer specializing in SaaS and AI companies, where he helps scale marketing operations, drive demand generation, and implement AI-powered strategies. He is the founder and publisher of Martech Zone, a leading publication in marketing technology, and a trusted advisor to startups and enterprises… More »
Back to top button
Close

Adblock Detected

We rely on ads and sponsorships to keep Martech Zone free. Please consider disabling your ad blocker—or support us with an affordable, ad-free annual membership ($10 US):

Sign Up For An Annual Membership