Skip Navigation

[Resolved] Restrict custom post amount based on role

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 1 voice.

Last updated by katjaL 3 weeks, 1 day ago.

Assisted by: Minesh.

Author
Posts
#2798953

Hi,
I have this code to restrict posts based on roles. This code works well: users with any other than "pro" role can create only one post with the cred form. If "pro" you can create two posts.

Ok, now I want to add another role, "pro-two", that can also have the 2 posts. Would you please help me (without PHP skills) how I add the "pro-two" have the similar priviledges than "pro". Thank you.

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;
//validate if specific form
if ($form_data['id']==559)
{
$user = get_current_user_id();
$user_post_count = count_user_posts( $user , 'pro' );
$user_data = get_userdata($user);
$user_role = $user_data->roles;
if ( $user_role[0] == 'subscriber' && $user_post_count > 0)
{
//set error message for my_field
$errors['wpcf-user-validation']='Only one post possible';
}
if ( $user_role[0] == 'pro' && $user_post_count > 1)
{
//set error message for my_field
$errors['wpcf-user-validation']='Two posts possible.';
}
}
//return result
return array($fields,$errors);
}

#2799034

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to use the followin code and check if that help you to resolve your issue:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;
//validate if specific form
if ($form_data['id']==559)
{
$user = get_current_user_id();


$user_post_count = count_user_posts( $user , 'pro' );
$pro2_user_post_count = count_user_posts( $user , 'pro-two' );

$user_data = get_userdata($user);
$user_role = $user_data->roles;
if ( $user_role[0] == 'subscriber' && $user_post_count > 0)
{
//set error message for my_field
$errors['wpcf-user-validation']='Only one post possible';
}
if ( $user_role[0] == 'pro' && $user_post_count > 1) {
//set error message for my_field
$errors['wpcf-user-validation']='Two posts possible.';
}

if ( $user_role[0] == 'pro-two' && $pro2_user_post_count > 1) {
//set error message for my_field
$errors['wpcf-user-validation']='Two posts possible.';
}

}
//return result
return array($fields,$errors);
}
#2799584

Yes, thank you Minesh again for your great help!