To check if a user has a specific role, you have to get a list of their roles and see if the role is listed there.
Example function:
function user_has_role($user_id, $role_name) { $user_meta = get_userdata($user_id); $user_roles = $user_meta->roles; return in_array($role_name, $user_roles); }
Example usage:
$user_is_subscriber = user_has_role(get_current_user_id(), 'subscriber');
Related Posts:
- What’s the net::ERR_HTTP2_PROTOCOL_ERROR about?
- How can I find my php.ini on wordpress?
- $wpdb->update or $wpdb->insert results in slashes being added in front of quotes
- Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 71 bytes)
- WordPress Error : Upload folder is not writable. Export and file upload features will not be functional
- WordPress child theme style.css not working
- Child theme css not overriding parent
- Prioritize Visible Content WordPress
- do_shortcode not working
- WooCommerce action hooks and overriding templates
- Contact Form 7 – Custom Validation
- XAMPP, using port:81, cannot run localhost:81/mywebsite
- WordPress query single post by slug
- How to integrate WordPress with Angular 8 for website?
- Create a folder if it doesn’t already exist
- How to remove index.php from WordPress site URL
- CSS background images in WordPress
- WordPress visual composer not loading
- wordpress menu dropdown item not showing after first dropdown
- WordPress get_template_directory_uri() not returning https and only return http
- Replace ' and similar html codes with their correspondent character?
- How to prevent the “Confirm Form Resubmission” dialog?
- navigate back with PHP form submission
- How to fix “Headers already sent” error in PHP
- “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
- http://localhost:80 is not working on running Apache server through UniServer ZeroXIII
- How do I get PHP errors to display?
- Get the full URL in PHP
- How to force file download with PHP
- How to Set Category Page as Home Page in Prestashop
- Getting an error when I visit http://localhost
- PHP random string generator
- What are the differences in die() and exit() in PHP?
- Expected status code 200 but received 500
- Can you “compile” PHP code and upload a binary-ish file, which will just be run by the byte code interpreter?
- Invalid argument supplied for foreach()
- Remove the last character from a string
- How to prevent Browser cache for php site
- what does a .php?id=&value=value means?
- Setting up a PHP interpreter in PhpStorm
- Get Current URL in Magento and show something
- Weird PHP error: ‘Can’t use function return value in write context’
- How do I get a YouTube video thumbnail from the YouTube API?
- How to call function of one php file from another php file and pass parameters to it?
- SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
- Is it possible to run .php files on my local computer?
- MAMP “Apache couldn’t be started because port is in use.” AND “Can’t connect to local MySQL server through /tmp/mysql.sock
- Php include not working? function not being included
- PHP mail function doesn’t complete sending of e-mail
- Convert php array to Javascript
- Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
- Convert DateTime to String PHP
- Xampp Access Forbidden php
- File attachment with PHPMailer
- Warning: mysqli_query() expects parameter 1 to be mysqli, resource given
- Fatal error: Call to undefined function money_format()
- What is the MM/DD/YYYY regular expression and how do I use it in php?
- Fatal error: Maximum execution time of 30 seconds exceeded
- cURL request in Laravel
- PHP array printing using a loop
- How to send to BCC address when using PHPMailer to format MIME message for Gmail API?
- Is there any way to test PHP locally without installing a server?
- How can I write php code in notepad++?
- PHP Redirect with POST data
- Getting the screen resolution using PHP
- PHP: fopen() Permission denied
- How should I be setting browscap.ini file
- CodeIgniter: Unable to connect to your database server using the provided settings Error Message
- Returning JSON from a PHP Script
- Can I store images in MySQL
- MySQL Daemon Failed to Start – centos 6
- How to generate .json file with PHP?
- Call to a member function on a non-object [duplicate]
- Generate PDF from HTML PHP
- Call to a member function fetch_assoc() on boolean in
[duplicate] - How to use Memcached with PHP7?
- Remove empty array elements
- select count(*) from table of mysql in php
- Auto increment in phpmyadmin
- Redirect old php link to wordpress link in .htaccess
- What is the meaning of ‘OFFSET’ => 1 (WordPress)
- WordPress custom thumbnail size
- Random Question Mark Icons In WordPress Text
- What does arg mean in WordPress
- WordPress dFactory Cookie Notice Plugin
- How to use wp_enqueue_style() in my WordPress theme?
- About 404 during WordPress installation
- “The Events Calendar” plugin by Modern Tribe in WordPress not working
- WordPress Main Menu – Hide ‘Home’ link on homepage only
- Scalable wordpress setup
- WordPress – Display Random Number Between 190-250
- I have shifted my wordpress site from one host to another, after shift the website wp-admin is not open
- Custom Coupon type woocommerce wordpress
- Remove admin bar for subscribers
- How to give new users two specific user role options upon WordPress user registration
- How to Add a cutsom slug to my custom author role
- how to create a user rule that can only manage support tickets?
- Obtain wordpress user role
- Custom user roles doesn’t apply changes
- Include administrator in author list