wordpress-plugin-development-45

Customize User Roles – WordPress Plugin Template

I created this plugin from a plugin “cookbook” that I have used in my learning and then developed it a bit beyond. I made sure to add in as much easy to follow code as possible as well as examples and comments. One of the worst things I have seen in coding plugins is that there is a massive lack of comments to help you understand what is going on in the code.

This plugin is a template that is simple to follow with only one main php file and will show you how you can access the user roles in wordpress and create your own names and capabilities.

There are many custom user role plugins out there and this is just a starting point to get you somewhat familiar with how you can start to change and manipulate those roles. The options are endless.

Installation

  1. Upload the WEBINS-custom-role-capability-template folder to your /wp-content/plugins/ directory or alternatively upload the WEBINS-custom-role-capability-template.zip file via the plugin page of webinsights.ca by clicking ‘Download’ and selecting the zip from your computer.
  2. Install and activate the WEBINS plugin.
  3. Activate the WEBINS WordPress plugin through the ‘Plugins’ menu in WordPress.
  4. Use WEBINS to change the user roles on the user page.

As this plugin is a template, it works well with what code is there already and the new roles that are created by default are:

  • Forum Suspended
  • Forum Member
  • Forum Moderator
  • Forum Administrator
<?php
/**
*Plugin Name: WEBINS Custom Role and Capability Plugin Template
*Plugin URI: http://www.webinsights.ca
*Description: This plugin is a template to help you to customize user roles and permissions on your website.
*Version: 1.0
*Author: Matthew Schonewille
*Author URI: http://www.webinsights.ca
*License: GLP2
*License URI:  https://www.gnu.org/licenses/gpl-2.0.html
*Text Domain:  wporg
*Domain Path:  /languages
**/

/** ----------------------------------------------------------------------------
  *| Custom forums roles and capabilities class.
  *|----------------------------------------------------------------------------
*/
class WEBINS_Forum_Roles {

    /* PHP4 Constructor Method.  ---- WEBINS_FORM_Roles is a custom name with the prefix WEBINS */
    function WEBINS_Forum_Roles () {

      /* Register plugin activation hook. */
      register_activation_hook( __FILE__, array( &$this, 'activation' ) );

      /* Register plugin deactivation. */
      register_deactivation_hook( __FILE__, array( &$this, 'deactivation' ) );
}


/** --------------------------------------------------------------------------
  *| Plugin Activation method.. --- typical function but we chose the name WEBINS_activation for the name. Use your own unique prefix.
  *|----------------------------------------------------------------------------

  * @param  $role - (string) (Required) Role name.
  * @param  get_role  -Retrieve role object.
  * @param  add_role ( string $role, string $display_name, array $capabilities = array() ) -Add new/user role if role does not exist
  * @param  add_cap  - Adds a capability to the defined role
  * Example - add_role (forum_administrator is the $role, Forum Administrator is the $display_name, array includes the $capabilities, ie. publish_forum_topics )
*/

function activation() {

    /* Get the default administrator role. */
    $role =& get_role( 'administrator' );


    /* Create the forum administrator role. ----------------------------------*/
    add_role(
        'forum_administrator',
        'Forum Administrator',
        array(
            'publish_forum_topics',
            'edit_other_topics',
            'delete_forum_topics',
            'read_forum_topics',
        )
      );

      /* Create the forum moderator role. ----------------------------------*/
      add_role(
          'forum_moderator',
          'Forum Moderator',
          array(
              'publish_forum_topics',
              'edit_other_topics',
              'read_forum_topics',
          )
        );

      /* Create the forum member role. ----------------------------------*/
      add_role(
          'forum_member',
          'Forum Member',
          array(
              'publish_forum_topics',
              'read_form_topics',
          )
        );

      /* Create the forum suspended role. ----------------------------------*/
      add_role(
          'forum_suspended',
          'Forum Suspended',
          array( 'read_forum_topics' )
        );
}



/** --------------------------------------------------------------------------
*| Plugin Deactivation method.. --- typical function but we chose the name WEBINS_deactivation for the name. Use your own unique prefix.
*|----------------------------------------------------------------------------

  * @param  $role - (string) (Required) Role name.
  * @param  $roles_to_delete - required roles that we want to delete upon deactivation
  * @param  get_role  -Retrieve role object.
  * @param  add_role ( string $role, string $display_name, array $capabilities = array() ) -Add new/user role if role does not exist
  * @param  remove_cap  - Removes a capability to the defined role
  * Example - add_role (forum_administrator is the $role, Forum Administrator is the $display_name, array includes the $capabilities, ie. publish_forum_topics )
*/

function deactivation() {

    /* Get the default administrator role. ----------------------------------*/
    $role =& get_role( 'administrator' );

    /* Remove forum capabilities to the adminstrator role.  ----------------------------------*/
    if ( !empty( $role ) ) {
      $role->remove_cap( 'publish_forum_topics' );
      $role->remove_cap( 'edit_other_topics' );
      $role->remove_cap( 'delete_forum_topics' );
      $role->remove_cap( 'read_forum_topics' );
    }

    /* Set up an array of role to delete ----------------------------------*/
    $roles_to_delete = array(
      'forum_administrator',
      'forum_moderator',
      'forum_member',
      'forum_suspended',
    );

    /* Loop through each role, deleting the role if necessary. ----------------------------------*/
    foreach ( $roles_to_delete as $role ) {

      /* Get the users of the role. ----------------------------------*/
      $users = get_users( array( 'role' => $role ) );

      /* Check if there are no users for the role. ----------------------------------*/
      if ( count( $users ) <= 0 ) {

          /* Remove the role from the site.  ----------------------------------*/
          remove_role( $role );
      }
    }
  }
}

$forum_roles = new WEBINS_Forum_Roles();

?>

*Plugin Name: WEBINS Custom Role and Capability Plugin Template
*Plugin URI: http://www.webinsights.ca
*Description: This plugin is a template to help you to customize user roles and permissions on your website.
*Version: 1.0
*Author: Matthew Schonewille
*Author URI: http://www.webinsights.ca
*License: GLP2
*License URI: https://www.gnu.org/licenses/gpl-2.0.html
*Text Domain: wporg
*Domain Path: /languages

Customize User Roles – WordPress Plugin TemplateCustomize User Roles – WordPress Plugin TemplateCustomize User Roles – WordPress Plugin Template

Leave a Reply