• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Content Modeling for WordPress
    • WordPress Hidden Gems
    • Web Design
  • Work
    • MLIS Class Projects (2019-2022)
    • Portfolio (2002-2019)
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →

Stephanie Leary

Writer, WordPress consultant, recent MLIS grad

  • About
    • Press Kit
    • Presentations and Interviews
  • Contact Me

Changing roles’ capabilities

February 4, 2010 Stephanie Leary 1 Comment

If you need to change one or two capabilities, it’s relatively easy to do so with a few lines of code in a plugin or your theme functions file. For example, if we wanted to allow any logged-in user to view our private posts and pages, we would need to grant them two additional capabilities.

<?php
// allow subscribers to view private posts and pages
$PrivateRole = get_role('subscriber');
$PrivateRole -> add_cap('read_private_pages');
$PrivateRole -> add_cap('read_private_posts');
?>

The first line fetches the existing subscriber role (as an object) and assigns to it a variable. In the next two lines, we add the capabilities to read private posts and pages to our variable. That’s it! Any subscribers can now read your private content — and so can authors and contributors, whose roles include all the capabilities of subscribers. (Remember that editors and administrators already had these particular capabilities.)

Adding capabilities by hand, one at a time, will be a pain if you need to make a lot of changes. If that’s the case, I recommend Justin Tadlock’s Members plugin, which provides an elegant user interface for modifying roles, and also includes some great features for managing content permissions.

WordPress capabilities, permissions, Privacy, private, Users and Roles

Reader Interactions

Comments

  1. Ben says

    March 12, 2012 at 4:01 pm

    Totally worked great! Thanks so much!!!

    Reply

Leave a Reply Cancel reply

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

Footer

My Books

I’m a front end developer at Equinox OLI, working on open source library software. I was previously a freelance WordPress developer in higher education. You can get in touch here or on LinkedIn.

Copyright © 2023 Stephanie Leary · Contact