Private content 2.0

My plugin Private Content has been updated to 2.0 and now you have a new feature: the possibility to exclude an higher role’s user from reading a lower role’s content. In other words, when in version below 2.0 we created a note for Subscribers, the higher roles could read this notes — and also in version 2.0 it works in the same way. But now it’s possible to hide a note to an Administrator, for example, and only the targeted group of users can read it, e.g. the Authors group.

How did I accomplish that?

The plugin creates a new capability for each role, obviously excluding the Administrator role:

function ubn_private_add_cap() {
    global $wp_roles;
    $wp_roles->add_cap( 'editor',      'read_ubn_editor_notes'      );
    $wp_roles->add_cap( 'author',      'read_ubn_author_notes'      );
    $wp_roles->add_cap( 'contributor', 'read_ubn_contributor_notes' );
    $wp_roles->add_cap( 'subscriber',  'read_ubn_subscriber_notes'  );
}
register_activation_hook( __FILE__, 'ubn_private_add_cap' );

The new capabilites are created on plugin activation and with this we are all done. But what in case of upgrading from versions below 2.0?

In case of upgrading, the plugin performs another check:

function ubn_private_check_capability_exists() {
    $editor_role = get_role( 'editor' );
    if ( ! isset( $editor_role->capabilities['read_ubn_editor_notes'] ) ) {
        ubn_private_add_cap();
    }
}
add_action( 'init', 'ubn_private_check_capability_exists' );

It looks for the presence of the capability read_ubn_editor_notes and, if it’s not in the database, the plugin will run the function for adding the new capabilities.

As usual, I’ve added the uninstall.php file to delete the new capabilities from the database: we all like to have and keep it clean. Something that every plugin develper should do.

This plugin is hosted in the WordPress repository, but you can find the development version on GitHub. Forks are welcome! :)

Author: aldolat

A WordPress fanatic.