News & Resources

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

User Roles and Capabilities

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.

YDesign Services

We develop innovative and high-quality Free WordPress Themes, Premium WordPress Themes and WordPress Plugins for business, portfolio, corporate sites, blogs and communities. We take pride not only in the quality of our products and efficient code, but also our world-class support and customer service.

Comment

  1. Sam says:

    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. It’s difficult to find experienced people in this particular subject, but you sound like you know what you’re talking about! Thanks

  4. foolish says:

    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!

  5. Great information. Lucky me I found your site by accident (stumbleupon). I’ve book-marked it for later!

  6. Peculiar article, just wwhat I wanted to find.

  7. ransel lipat says:

    Hi, this weekend is pleasant in favor of me, as this occasion i am reading this impressive educational paragraph here at my residence.

  8. 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.

  9. single says:

    Very descriptive post, I enjoyed that bit. Will there
    be a part 2?

  10. track says:

    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!

  11. I am incessantly thought about this, appreciate it for posting.

  12. I love what you guys are usually up too. This kind of clever work and coverage!

    Keep up the amazing works guys I’ve included you guys
    to blogroll.

  13. I am glad to be a visitant of this complete site, regards
    for this rare information!

  14. Doctorshoper says:

    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.

  15. 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.

  16. Doctorshoper says:

    Good information. Lucky me I discovered your blog by chance (stumbleupon).
    I have bookmarked it for later!

  17. 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.

  18. erotica says:

    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.

  19. dirty talk says:

    If you want to grow your knowledge just keep visiting this site and be updated with the
    most recent news update posted here.

  20. 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.

  21. Hi there, just wanted to mention, I liked this article. It was funny.

    Keep on posting!

  22. businesslogr says:

    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.

  23. Hi there friends, how is all, and what you desire to say on the topic of this
    post, in my view its really amazing for me.

  24. 수원오피 says:

    This info is priceless. When can I find out more?

  25. Asking questions are really nice thing if you are not understanding something totally, except this piece of
    writing offers good understanding yet.

  26. 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..

  27. Altrina says:

    I am not really superb with English but I find this rattling easygoing to read.

  28. 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

  29. 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.

  30. 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.

  31. Jolique says:

    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.

  32. Hello, after reading this awesome piece of writing i am
    too cheerful to share my know-how here with mates.

  33. 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!

  34. wptags says:

    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.

  35. Arlette says:

    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.

  36. Everything is very open with a very clear explanation of the challenges.
    It was truly informative. Your site is extremely helpful.
    Many thanks for sharing!

  37. 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.

  38. 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.

  39. 천안오피 says:

    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.

  40. kansas says:

    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.

  41. fatima hand says:

    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!

  42. 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?

  43. overstock says:

    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.

  44. Hi friends, how is all, and what you want to say concerning this piece of writing, in my view its
    in fact amazing designed for me.

  45. Smokeshop says:

    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.

  46. I every time spent my half an hour to read this weblog’s articles
    or reviews everyday along with a cup of coffee.

  47. crown says:

    Hi there, I enjoy reading through your post. I wanted to write a little comment to support you.

  48. 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.

  49. 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!

  50. wood says:

    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.

  51. Hello to every body, it’s my first pay a visit of this webpage; this web site consists of amazing and truly excellent material designed for readers.

  52. Very nice write-up. I absolutely love this site. Stick with it!

  53. Hi there Dear, are you actually visiting this web site on a regular basis,
    if so after that you will absolutely obtain nice know-how.

  54. 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?

  55. My brother recommended I might like this web site. He was entirely right.
    This post truly made my day. You cann’t imagine just how much time I had spent for this information! Thanks!

  56. Pretty! This was a really wonderful post. Thanks for supplying these
    details.

  57. Very nice article. I definitely love this
    website. Stick with it!

Leave a Reply

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