4

I am creating a Wordpress website for multi author and want to set user role as per article submission. Means if any user have 0-10 article they will go to Contributor role, if 11-30 will go to Author role if 31-100 will go to Editor role.

Also I want to make registration system where default registration group will be Subscriber. They will get a link into verification email like

If you want to become a Contributor please click on below link. (To submit an article you must have at least Contributor permission) http:// link will be here ... this link automatically change user role from Subscriber to Contributor.

Hope I will get solution from you expert. I am posting this issue with lots of hope from you friends.

Code Lover
  • 8,099
  • 20
  • 84
  • 154

2 Answers2

8

What you want to do is when they post their submission check to see how many posts they have authored and then change the role. So in your theme's functions.php file you'd need a hook that is like this.

add_action('publish_post', 'update_roles');

and then a function to update the roles.

function update_roles()
{

   global $wpdb;

   // Get the author
   $author = wp_get_current_user();

   // Not sure if $author and $u are the same object I suspect they are. 
   // so this may not be necessary, but I found this code elsewhere.
   // You may be able to do without this and just replace $u with $author later in the code.
   // Get post by author
   $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

   $numPost = count($posts);

   // Do the checks to see if they have the roles and if not update them.
   if($numPost > 0 && $numposts <= 10 && current_user_can('subscriber'))
   {
       // Remove role
       $author->remove_role( 'subscriber' );

       // Add role
       $author->add_role( 'contributor' );

   }

   ...... other conditions .......

}
thenetimp
  • 9,487
  • 5
  • 29
  • 42
  • Thanks a lot for the code... Just one quick question.. will it change user role permanently or just only will check while posting and than back to contributor? In fact I will try and see practically but just ask if you know that.. – Code Lover Feb 08 '12 at 03:41
  • That will change it permanently. If you want to change it back you'll need to work out those conditions for changing it back. – thenetimp Feb 08 '12 at 07:34
  • I want change it to permanently so this is exactly what I am looking for. Thanks thenetimp for your help, I will try this code and get back to you. – Code Lover Feb 08 '12 at 08:22
  • i made a quick change. Removed the ! in the conditional, you may want to play with that logic a bit but the basic idea is there, this code isn't 100% you will need to tinker with it around what you need, but it's a good basis to start from imo – thenetimp Feb 08 '12 at 09:06
  • Its giving syntax error on this line `if($numPost > 0 && $numposts is <= 10 && current_user_can('subscriber'))` – Code Lover Feb 09 '12 at 06:58
  • What it says there is if a person is a subscriber, and they have more than 0 posts and less than or equal to 10 posts remove their subscriber role and add a contributor role. You'll have to do conditional statements for each of the conditions you want to meet. – thenetimp Feb 09 '12 at 07:51
  • `Parse error: syntax error, unexpected T_STRING` this is the error message and this is the code what I have added `if($numPost > 0 && $numposts is <= 2 && current_user_can('contributor')) { // Remove role $u->remove_role( 'contributor' ); // Add role $u->add_role( 'author' ); } if($numPost > 2 && $numposts is <= 4 && current_user_can('author')) { // Remove role $u->remove_role( 'author' ); // Add role $u->add_role( 'editor' ); }` – Code Lover Feb 09 '12 at 09:41
  • OOP... remove the word "is" from the if statements. My bad. – thenetimp Feb 09 '12 at 10:05
  • Now another error got when trying to approve and publish contributor's post. `Warning: Missing argument 1 for get_userdata(), called in C:\xampp\htdocs\mysite\wp-content\themes\mytheme\functions.php on line 28 and defined in C:\xampp\htdocs\sharetome\wp-includes\pluggable.php on line 103 Fatal error: Call to a member function get_results() on a non-object in C:\xampp\htdocs\mysite\wp-content\themes\mytheme\functions.php on line 36` – Code Lover Feb 09 '12 at 11:14
  • i just made some mods to get the current user as I would hope he/she is logged in at the time, so that should help. I also changed $u to be $author. – thenetimp Feb 09 '12 at 11:59
  • getting error while approve/publish contributor post `Fatal error: Call to a member function get_results() on a non-object in C:\xampp\htdocs\mysite\wp-content\themes\mytheme\inc\functions\misc-functions.php on line 49` and 49 line is `$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );` – Code Lover Feb 09 '12 at 18:43
  • ah sorry fogot to add wpdb as a global try it now. (how much should I charge you for this?) ;-) – thenetimp Feb 09 '12 at 18:59
  • Now no error but its not updating/changing any user role as per post count. :( – Code Lover Feb 09 '12 at 19:56
  • This is the general idea, you'll have to play around with some debug code to figure out why it's not getting into the logic. – thenetimp Feb 09 '12 at 19:58
  • i just saw a problem with the if statement $numposts should be $numPosts see if that fixes the problem. – thenetimp Feb 14 '12 at 19:44
  • I have modified but no change.. :( its not changing role at all. – Code Lover Feb 14 '12 at 20:36
0

Using SQL statements (Database Queries) to get at Wordpress data is not in accordance with Wordpress coding standards . See Wordpress Handbook

It would be better to use count_user_posts function

See Function on the codex

CommandZ
  • 3,333
  • 1
  • 23
  • 28