Title: custom role creation a plugin
Last modified: September 1, 2016

---

# custom role creation a plugin

 *  [dourvas](https://wordpress.org/support/users/dourvas/)
 * (@dourvas)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/)
 * Ok.
    I really need the experiece of the community to accomplish a task
 * i am a little comfused about membership plugins and user role creation plugins
 * So i am writing what i need and i am expecting a tip about how to accomplish 
   that
 * I want to create a new role.
    This role must own the above feutures: -his own
   registration page from the front-end -ability to create a specific amount of 
   users of a specific role
 * ( a teacher can create accounts himself for 10 students)
 * what do i need?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [dimitris33](https://wordpress.org/support/users/dimitris33/)
 * (@dimitris33)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/#post-7528232)
 * I think you are looking for something like this [https://managewp.com/create-custom-user-roles-wordpress](https://managewp.com/create-custom-user-roles-wordpress)
 *  Thread Starter [dourvas](https://wordpress.org/support/users/dourvas/)
 * (@dourvas)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/#post-7528282)
 * thanks for your answer.
 * I will write the actions i have allready done about that task and the thoughts
   i am having about its completion
 * I have created a custom role. I used MEMBERS plugin. I named the role manager
   and i gave him the capability to create users and to edit users.
 * I remind you that i want him to be able to create a SPECIFIC number of a SPECIFIC
   role.
 * I have a thought and i want some advice here. I should mention that i can code
   but i am new in WP. That is a solution a would probably follow if i had chosen
   to built this custom and not through WP
 * I am thinking to first go to database and add the field MANAGERID to users table
   
   Then i am considering of building a form. I mean Pages –> New –> html tab –> 
   and build a page with a form.
 * In this new page i will restrict the access only to members with the role of 
   manager. (i have already tried that succesfully using a memberhip plugin)
 * This form should contain the necceserry fields of a new user. Submiting the form
   the data goes to table alongdide with the managerid of the manager.
 * In that way i can count the users each manager created and i can both
    -manage
   the edit to the right users and -manage to limit the number of users created.
 * I do not have a clue how to set the roles for the created users
 * Is that a valid doable solution?
    I am really open to any other suggestions, 
   tips or anything relevant
 *  [dimitris33](https://wordpress.org/support/users/dimitris33/)
 * (@dimitris33)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/#post-7528343)
 * Hi again, could you post which plugin is this? a link to it?
 * i am not sure if there is a plugin free or premium that does what you need but
   if you are not a proficient in programming i would advise to avoid making changing
   in core files or in the database, at least manually.Better create a plugin that
   will do that.
 *  Thread Starter [dourvas](https://wordpress.org/support/users/dourvas/)
 * (@dourvas)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/#post-7528349)
 * Thanks for the answer.
    I surely do not have a plugin like this. I just mentioned
   my thoughs about dealing with the issue I descriped in my previous post.
 * I understand your feedpack about changing the datadase. I will try to think of
   another solution.
    If you have an algorithm about solving that please share
 *  [dimitris33](https://wordpress.org/support/users/dimitris33/)
 * (@dimitris33)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/#post-7528368)
 * In this page [https://codex.wordpress.org/Roles_and_Capabilities#Plugins](https://codex.wordpress.org/Roles_and_Capabilities#Plugins)
   you will find information about how WP handles user roles and plugins recommended
   to help you started.
 *  Thread Starter [dourvas](https://wordpress.org/support/users/dourvas/)
 * (@dourvas)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/#post-7528379)
 * Ok.
    I have implemented the task. I am writing the solution i came up for two
   reasons -i reaaly need some feedback about the solution ( i am new in WP and 
   i do not want to find any suprises i didn’t see when the system is up and running)-
   To share with other members that perhaps are looking for something like this
 * Steps
    - i installed MEMBERS plugin. I created two custom roles with names coyotemanager
      and coyotestudent. I gave to the coyotemanger the capability to create and
      edit users.
    - I installed the plugin MEMBERSHIP 2. i created 2 memberships. Manager and 
      student. I connected those memberships with the same roles using this code(
      put in functions.php of your theme)
    -     ```
          add_action( 'ms_model_relationship_create_ms_relationship_before', 'ms_controller_member_assign_memberships_done_cb', 99, 4 );
          function ms_controller_member_assign_memberships_done_cb( $membership_id, $user_id, $gateway_id, $move_from_id ) {
          	$user = new WP_User( $user_id );
          	switch( $membership_id ){
          		case 5814: //found it in MEMBERSHIP 2
          			$user->set_role( 'coyotemanager' );
          			break;
      
          		case 5844:
          			$user->set_role( 'coyotestudent' );
          			break;
          	}
          }
          ```
      
    -  I created a WP page. I gave access to this page only to coyotemanager.
    - I wrote a php snippeset. the current user (coyotemanger only) can create a
      specific amount (you specify it into the code) of students.
    -     ```
          <?php
          $servername = "localhost";
          $username = "root";
          $password = "";
          $dbname = "ld";
      
          $conn = new mysqli($servername, $username, $password, $dbname);
          // Check connection
          if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
          }
      
          //get the number of students//////////////////////////////////////////////
          $sql="SELECT * FROM wp_usermeta
          WHERE meta_key ='description' and meta_value='".get_current_user_id()."'";
          $result = $conn->query($sql);
          	$num_students = mysqli_num_rows($result);
          	echo "Σύνολο μαθητών σου:".$num_students."";
          ///////////////////////////////////////////////////////////////////////////
      
          //create sql to get the name and lastname od teacher's student////////////////////
          $i=1;
              while($row = $result->fetch_assoc()) {
          	   $query[$i] = "SELECT * FROM wp_usermeta WHERE user_id ='".$row["user_id"]."'";
          		//echo  "query=".$query[$i];
          		$i++;
      
          	}
          ///////////////////////////////////////////////////////////////////////////////////
      
          //run the queries get name and last name and print///////////////////////////////////////
          $j=1;
              while ($j !== $num_students+1)
          	{
              $result1 = $conn->query($query[$j]);
          		while($row = $result1->fetch_assoc()) {
      
          		    if ($row["meta_key"] === "first_name"){
          		      $userdataname= $row["meta_value"];
          		     }
      
          		    if ($row["meta_key"] === "last_name"){
          		     $userdatalname = $row["meta_value"];
          		     }
      
                  }
          		 echo "id: " . $row["user_id"]. " - Name: " . $userdataname. " " . $userdatalname. "";
          		$j++;
          	}		
      
          	if ($num_students <3)// 3 users
          	{
          	echo "may add still ". 3-$num_students."μαθητές
      
          ";	
      
          	echo'	<form name="usercreation" method="POST" onsubmit="return form_validation()" action="../usercreation.php">
                  Name: <input type="text" id="student_name" name="student_name" />
                  Last Name: <input type="text" id="student_lname" name="student_lname" />
                  Email: <input type="text" id="student_email" name="student_email" />
                  username: <input type="text" id="username" name="username" />
      
                  <input type="submit" value="Submit"/>
               </form>';
      
          } else {
              echo "No more users";
          }
          $conn->close();
      
          ?>
          ```
      
    - The php file usercreation.php (form action)
    -     ```
          <?php
          // Get data
          require_once('wp-load.php');
      
          $student_name = $_POST["student_name"];
          $student_lname = $_POST["student_lname"];
          $student_email = $_POST["student_email"];
          $username = $_POST["username"];
      
          if( null == username_exists( $username ) ) {
      
            // Generate the password and create the user
            $password = wp_generate_password( 12, false );
            $managerid= get_current_user_id();
            $userdata = array(
              'user_login'  => $username,
          	'description'  => $managerid, //store the manager's id into students data
              'first_name'  => $student_name,
          	'last_name'   => $student_lname,
              'user_pass'   => $password,
          	'user_email'  => $student_email,
          	'role'   => "coyotestudent"
          );
      
          $user_id = wp_insert_user( $userdata ) ;
      
          //On success
          if ( ! is_wp_error( $user_id ) ) {
              echo "User created : ". $user_id;
          }
          else{
          	 $error_string = $user_id->get_error_message();
             echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
      
          }
      
             // Email the user
           wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password ); 
      
          } // end if
      
          ?>
          ```
      
    - Still needs form validation and css

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘custom role creation a plugin’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [dourvas](https://wordpress.org/support/users/dourvas/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/custom-role-creation-a-plugin/#post-7528379)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
