How PHP helps in changing User Roles and Capabilities in WordPress?

WordPress is known to come with a powerful yet simple user management system where users have capabilities which are based on the specific roles they are assigned to. WordPress comes with the following five default user roles and capabilities:

– Administrator
– Editor
– Author
– Contributor
– Subscriber

You can also see full list of user roles on wordpress.org

All these roles come with predefined capabilities but they can be easily changed with the help of coding or plugins such as Members, Capability Manager Enhanced or User Role Editor. These plugins are great to use for wordpress users, who don’t want to get into coding stuff. But if you think you are in a “pro-developer” mode then few lines of PHP will help you out in this.

Change WordPress User Capabilities with PHP

A wordpress role can be modified through an accessory function in PHP which is called get_role. Following code can allow Editor to remove_users:

function wps_editor_can_remove_users() {
$editor = get_role( 'editor' );
$editor->add_cap( 'remove_users' );
}

The fetch of an ‘Editor’ Role takes care of the ‘editor’ parameter on the add_cap method. You can make use of this method in your plugins or themes’s function.php. Whatever changes you do are directly stored to the database.

Create New WordPress User Role with PHP

You can use add_role function in PHP to change the capabilities of existing roles. It includes three parameters:

– Role slug-like name – ‘comment_moderator’
– Role display name – ‘Comment Moderator’
– Array of Capabilities for User

Here is example for the code:

register_activation_hook( __FILE__, 'wps_add_roles_on_activation' );
function wps_add_roles_on_activation() {
add_role(
'comment_moderator',
__('Commment Moderator', 'plugin-slug'),
array(
'read' => true,
'edit_posts' => true,
'edit_other_posts' => true,
'edit_published_posts' => true,
'moderate_comments' => true
)
);
}

Remember that roles must only be created when your plugin is activated because these new roles are stored in the database and are anytime reloaded by WordPress. Don’t worry, as nothing will be broken if you have them hooked in the init action.

Change Specific User Capabilities

Sometimes, there is a need to give few or more powers to a specific user. Methods of add_cap and remove_cap can be used to change a specific user’s capabilities in WordPress. Here is an example for this:

add_action( 'init', 'wps_david_cant_edit' );
function wps_david_cant_edit() {
$user_id = 7; // The ID of the user
$user = new WP_User( $user_id );
$user->remove_cap( 'edit_posts' );
}

Once the role’s general capabilities have been loaded, this is a specific override on a specific user.

WordPress comes with pre defined set of user roles and capabilities that determines what a user can do on the website. Playing with the User roles and capabilities is just another area that one can explore.

SHARE:

Facebook
Twitter
Pinterest
LinkedIn
Reddit
Email

58 Responses

  1. Simply wish to say your article is as amazing. The clarity to your post is simply nice and i can think you’re knowledgeable in this subject.

    Fine together with your permission allow me to clutch your RSS feed to keep up to date with forthcoming post. Thanks 1,000,000 and please keep up the enjoyable work.

  2. Thanks for any other informative site. The place else may I get that kind of information written in such an ideal manner? I have a challenge that I’m simply now working on, and I’ve been on the glance out for such info.

  3. Wonderful web site. Plenty of useful info here. I’m sending it to some friends ans also sharing in delicious. And certainly, thanks for your effort!

  4. I got this web site from my buddy who told me on the topic of this website and now this time I am visiting this site and reading very informative articles or reviews here.

  5. My programmer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on various websites for about a year and am concerned about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress posts into it?

    Any help would be really appreciated!

  6. Hmm it seems like your blog ate my first comment (it was extremely long) so I guess I’ll just sum
    it up what I wrote and say, I’m thoroughly enjoying your
    blog. I too am an aspiring blog writer but I’m still new
    to everything. Do you have any helpful hints for novice blog writers?
    I’d genuinely appreciate it.

  7. whoah this weblog is excellent i love reading your articles.
    Keep up the good work! You know, a lot of individuals are looking around for this info, you can help
    them greatly.

  8. A lot of thanks for your whole labor on this website. My niece takes pleasure in working on investigations and it’s simple to grasp why.
    My partner and i notice all of the dynamic form you provide very important guides through the web site and even strongly encourage response from website visitors on that article while our girl has been understanding a whole lot.
    Take advantage of the remaining portion of the new year.
    You have been conducting a fantastic job.[X-N-E-W-L-I-N-S-P-I-N-X]I
    am extremely impressed along with your writing skills as well as with the structure
    to your weblog. Is this a paid theme or did you modify it yourself?

    Anyway stay up the nice high quality writing, it’s uncommon to look a nice
    weblog like this one these days.

  9. whoah this weblog is great i love studying your posts.

    Keep up the great work! You already know, a lot of individuals are searching
    around for this information, you could help them greatly.

  10. Normally I do not read article on blogs,
    however I wish to say that this write-up
    very pressured me to try and do so! Your writing style has been surprised me.

    Thanks, very great article.

  11. It’s in reality a great and useful piece of info.
    I am happy that you just shared this useful information with us.
    Please keep us up to date like this. Thanks for sharing.

  12. I am extremely inspired with your writing skills and also with the format in your blog.
    Is that this a paid subject matter or did you modify it
    your self? Either way keep up the nice high quality writing, it’s rare to look a great blog like this one nowadays..

  13. I believe this is among the such a lot vital info for
    me. And i’m glad reading your article. But want to observation on some normal issues, The website style is great, the
    articles is actually excellent : D. Just right activity, cheers

  14. Aw, this was an exceptionally good post. Taking the time and actual effort to produce a very good article?
    but what can I say? I put things off a whole lot and never seem to get nearly anything done.

  15. After looking over a number of the blog articles on your web site, I truly
    appreciate your way of writing a blog. I saved it to my bookmark website
    list and will be checking back soon. Please visit my website too and tell me
    what you think.

  16. I just now wanted to thank you once again for this amazing web-site you have developed here.

    It’s full of ideas for those who are truly interested in that subject,
    primarily this very post. You’re really all so sweet
    along with thoughtful of others and reading your blog posts is a superb delight to me.
    And what a generous reward! Tom and I are going to have excitement making use of your tips
    in what we should do in a month’s time. Our checklist is
    a mile long and tips might be put to beneficial use.

  17. I’m really enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more pleasant for
    me to come here and visit more often. Did you hire out a developer to create your theme?

    Superb work!

  18. Aw, this was a really nice post. Finding the time and actual effort to generate
    a good article… but what can I say… I hesitate a lot and never
    manage to get anything done.

  19. What’s up everyone, it’s my first go to see at this web page, and
    article is really fruitful designed for me, keep
    up posting such content.

  20. We are a group of volunteers and opening a new scheme in our community.
    Your website offered us with valuable information to work on. You’ve done an impressive job and our whole community will be grateful to you.

  21. I’ve been surfing online more than three hours today,
    yet I never found any interesting article like yours.
    It’s pretty worth enough for me. Personally, if all
    web owners and bloggers made good content as you did, the web will be
    much more useful than ever before.

  22. What’s up it’s me, I am also visiting this
    site on a regular basis, this website is genuinely nice and
    the users are genuinely sharing fastidious thoughts.

  23. Your style is so unique in comparison to other folks I’ve read stuff from.
    Many thanks for posting when you have the opportunity,
    Guess I’ll just bookmark this web site.

  24. I used to be suggested this website via my cousin. I am no longer positive whether this post is written by him as no one
    else recognize such distinct about my problem.
    You are incredible! Thank you!

  25. I leave a comment when I especially enjoy a post on a site
    or if I have something to valuable to contribute to the discussion. Usually it’s triggered by
    the passion communicated in the article I browsed. And after this article
    How PHP helps in changing User Roles and Capabilities in WordPress?
    -. I was actually excited enough to drop a thought 🙂 I actually do have some questions for you if
    you usually do not mind. Could it be just me or does it look like a few of the responses look like written by
    brain dead people? 😛 And, if you are writing at other
    places, I would like to follow everything new
    you have to post. Would you make a list the complete urls of all your social pages like your twitter feed, Facebook page or linkedin profile?

  26. Hello There. I found your blog using msn. This is a
    really well written article. I will make sure to bookmark
    it and return to read more of your useful info.
    Thanks for the post. I will certainly return.

  27. An impressive share! I have just forwarded this onto a colleague who has been conducting a
    little homework on this. And he actually bought me
    lunch because I found it for him… lol. So let me reword this….
    Thanks for the meal!! But yeah, thanks for spending time
    to talk about this matter here on your web site.

  28. Thank you for sharing superb informations. Your web site is so cool.
    I’m impressed by the details that you’ve on this web site.
    It reveals how nicely you perceive this subject.
    Bookmarked this web page, will come back for more articles.

    You, my friend, ROCK! I found just the info I already searched everywhere
    and just could not come across. What a perfect web-site.

  29. Hi there! I could have sworn I’ve been to this web site
    before but after browsing through a few of the posts I realized it’s new to me.
    Anyhow, I’m definitely happy I came across it and I’ll
    be book-marking it and checking back often!

  30. whoah this blog is wonderful i love studying your posts.
    Stay up the good work! You already know, a lot of people
    are looking round for this info, you could help them greatly.

  31. I do not create many remarks, but i did a few searching and wound up here How PHP helps in changing User Roles and Capabilities in WordPress?

    -. And I actually do have a few questions for you if it’s allright.
    Could it be only me or does it look as if like some of these remarks come across like written by brain dead folks?
    😛 And, if you are writing at other places, I would like to keep up with everything new
    you have to post. Would you make a list of every one
    of your community sites like your twitter feed, Facebook page or linkedin profile?

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media

Most Popular

Get The Latest Updates

Subscribe To Our Weekly Newsletter

No spam, notifications only about new products, updates.
On Key

Related Posts