Skip Navigation

[Resolved] Limit the number of CRED form Submissions

This thread is resolved. Here is a description of the problem and solution.

Problem:

I had followed your recommendation at https://toolset.com/forums/topic/limiting-cred-form-submission-to-one-post/#

I have a CRED form (ID: 1695) which creates CPT Reviews (review) of another parent CPT Shops (shop) and I want to limit the number of reviews each user on my site submits, down to one review per shop.

The user roles are as follows:

Role 1: Author
Role 2: Contributor
Role 3: Editor

and I would like to show the same error message.

Solution:

You can add an author filter into the query, for example,

//Limit the number of reviews for a user on a parent CPT
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;
     
    //uncomment this if you want to print the field values
    //print_r($fields);
     
    //validate if specific form
    if ($form_data['id']==1695)
    {       
            $user = get_current_user_id();
            $shop_id = $_POST['_wpcf_belongs_shop_id'];
            $args = array(
                'meta_key'   => '_wpcf_belongs_shop_id',
                'meta_value' => $shop_id,
                'post_type'  => 'review',
                'author' => get_current_user_id()
            );
            $query = new WP_Query( $args );
            $user_post_count = 0;
            if(isset($query->found_posts)){
                $user_post_count = $query->found_posts;
            }
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
               
         if ( in_array($user_role[0], array('subscriber', 'author', 'contributor', 'editor')) && $user_post_count > 0)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='<span><i class="fa fa-star" aria-hidden="true"></i> You have already left a Review!</span>';
        }
    }
    //return result
    return array($fields,$errors);
}

Relevant Documentation:

https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
https://developer.wordpress.org/reference/functions/get_current_user_id/

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by Luo Yang 6 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#602794

Hi,

I had followed your recommendation at https://toolset.com/forums/topic/limiting-cred-form-submission-to-one-post/#

I have a CRED form (ID: 1695) which creates CPT Reviews (review) of another parent CPT Shops (shop) and I want to limit the number of reviews each user on my site submits, down to one review per shop.

The user roles are as follows:

Role 1: Author
Role 2: Contributor
Role 3: Editor

and I would like to show the same error message.

This code was my poor attempt, can you help? Thanks

//Limit the number of reviews for a user
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;
   
    //uncomment this if you want to print the field values
    //print_r($fields);
   
    //validate if specific form
    if ($form_data['id']==1695)
    {       
            $user = get_current_user_id();
            $user_post_count = count_user_posts( $user , 'review' );
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
             
        if ( $user_role[0] == 'author' && $user_post_count > 0)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Only 1 review per shop is allowed';
        }
    }
   
    //return result
    return array($fields,$errors);
}
#602860

Dear Tino,

You can try to modify the codes from:
if ( $user_role[0] == 'author' && $user_post_count > 0)

To:
if ( in_array($user_role[0], array('author', 'contributor', 'editor')) && $user_post_count > 0)

More help:
hidden link
in_array — Checks if a value exists in an array

#602924

Hi Luo,

Thanks, your suggestion worked, but I still require further help. The following is my latest code:

//Limit the number of reviews for a user
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;
   
    //uncomment this if you want to print the field values
    //print_r($fields);
   
    //validate if specific form
    if ($form_data['id']==1695)
    {       
            $user = get_current_user_id();
            $user_post_count = count_user_posts( $user , 'review' );
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
             
         if ( in_array($user_role[0], array('author', 'contributor', 'editor')) && $user_post_count > 0)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Only 1 review per product is allowed';
        }
    }
   
    //return result
    return array($fields,$errors);
}

The above code limits the user to one review on the entire site. What is happening now is, when a user goes to another product (Parent CPT = shop) and wants to leave a review the error message is displayed.

How can we adjust this code to limit and allow one Review (Child CPT) per Shop (Parent CPT) ?

Thanks

#603253

I assume the parent post type "Shop" is using slug "shop", for example, you can modify your codes from:

			$user_post_count = count_user_posts( $user , 'review' );

To:

			$shop_id = $_POST['_wpcf_belongs_shop_id'];
			$args = array(
				'meta_key'   => '_wpcf_belongs_shop_id',
				'meta_value' => $shop_id,
				'post_type'  => 'review'
			);
			$query = new WP_Query( $args );
			$user_post_count = 0;
			if(isset($query->found_posts)){
				$user_post_count = $query->found_posts;
			}
#603364

Thank you for your superb help Luo, it worked perfectly. Here is my final code for any body else wanting the solution.

//Limit the number of reviews for a user on a parent CPT
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;
    
    //uncomment this if you want to print the field values
    //print_r($fields);
    
    //validate if specific form
    if ($form_data['id']==1695)
    {       
            $user = get_current_user_id();
			$shop_id = $_POST['_wpcf_belongs_shop_id'];
			$args = array(
				'meta_key'   => '_wpcf_belongs_shop_id',
				'meta_value' => $shop_id,
				'post_type'  => 'review'
			);
			$query = new WP_Query( $args );
			$user_post_count = 0;
			if(isset($query->found_posts)){
				$user_post_count = $query->found_posts;
			}
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
              
         if ( in_array($user_role[0], array('subscriber', 'author', 'contributor', 'editor')) && $user_post_count > 0)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='<span><i class="fa fa-star" aria-hidden="true"></i> You have already left a Review!</span>';
        }
    }
    //return result
    return array($fields,$errors);
}
#603461

Hi Luo,

After further testing I still require assistance.

The code above allows only one review in one shop to be reviewed by only one user.

Ex: When I try to submit another review to the same shop using another user, then the error message appears restricting submission of their first review.

We need to enable multiple users to review one shop, and if they try to submit another review for that same shop, then the error message appears.

#603651

You can add an author filter into the query, for example, replace this from:

            $args = array(
                'meta_key'   => '_wpcf_belongs_shop_id',
                'meta_value' => $shop_id,
                'post_type'  => 'review'
            );

To:

            $args = array(
                'meta_key'   => '_wpcf_belongs_shop_id',
                'meta_value' => $shop_id,
                'post_type'  => 'review',
                'author' => get_current_user_id()
            );

And test again.

More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
https://developer.wordpress.org/reference/functions/get_current_user_id/

#603754

Thank you so much Luo. It works perfectly.

My final code:

//Limit the number of reviews for a user on a parent CPT
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;
    
    //uncomment this if you want to print the field values
    //print_r($fields);
    
    //validate if specific form
    if ($form_data['id']==1695)
    {       
            $user = get_current_user_id();
			$shop_id = $_POST['_wpcf_belongs_shop_id'];
			$args = array(
				'meta_key'   => '_wpcf_belongs_shop_id',
				'meta_value' => $shop_id,
				'post_type'  => 'review',
				'author' => get_current_user_id()
			);
			$query = new WP_Query( $args );
			$user_post_count = 0;
			if(isset($query->found_posts)){
				$user_post_count = $query->found_posts;
			}
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
              
         if ( in_array($user_role[0], array('subscriber', 'author', 'contributor', 'editor')) && $user_post_count > 0)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='<span><i class="fa fa-star" aria-hidden="true"></i> You have already left a Review!</span>';
        }
    }
    //return result
    return array($fields,$errors);
}
#604164

Your are welcome