Skip Navigation

[Resolved] Restrict form per user (limit)

This support ticket is created 5 years, 11 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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by JakubV7709 5 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1183147

Hi Toolset team,

i just want to setup limit of post type per user. I use this php code in function.php, which i found on your support forum and it works perfect. But I would like to add limit for another form into this php code, but im not able to setup this code :-/ could you help me with this? When i copied this code two times (and of course change name of form and id) it didnt work for me.

I need to add into this php code same function for another form - for example (name of form: cars, id of form: 299).

I know that for you guys it will take maybe just minute to setup.

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']==288)

{
$user = get_current_user_id();
$user_post_count = count_user_posts( $user , 'article' );
$user_data = get_userdata($user);
$user_role = $user_data->roles;

if ( $user_role[0] == 'vendor' && $user_post_count > 3)
{
//set error message for my_field
$errors['wpcf-omezeni']='If you want more article contact us';
}

if ( $user_role[0] == 'premium' && $user_post_count > 999)
{
//set error message for my_field
$errors['wpcf-user-validation']='No limit';
}

//check if featured image exists

}

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

#1183464

Hi Jakub,

Thank you for contacting us and I'll be happy to assist.

To extend the custom function to execute for multiple forms, you can update the following code lines from:


//validate if specific form
if ($form_data['id']==288)

To:


//validate if specific forms
if ( ($form_data['id']==288) || ($form_data['id']==299) )

This will make the function work for forms with ID "288" and "299".

I hope this helps and please let me know how it goes.

regards,
Waqar

#1183583

Thanks for quick reply... but.. im not sure that i am explain it well. I will try it explain better and again.

I use php code for limit per user per article - so i dont want to allow user send via form more then 3 articles. That works great!

But i want to add function, where i will setup limit for another form (for example: jobs). So i need add to php option where i will setup how many jobs can user send me via form.

Im not sure if this code below is correct - should you check it? Please focus on $user_post_count2.

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;
    
    //uncomment this if you want to print the field values
    //print_r($fields);
    
    //validate if specific form
    if ( ($form_data['id']==288) || ($form_data['id']==375) )
      
    {       
            $user = get_current_user_id();
            $user_post_count = count_user_posts( $user , 'article' );
            $user_post_count2 = count_user_posts( $user , 'job' );
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
              
        if ( $user_role[0] == 'vendor' && $user_post_count > 3)
        {
            //set error message for my_field
            $errors['wpcf-omezeni']='If you want more article contact us';
        }
        
          if ( $user_role[0] == 'premium' && $user_post_count > 999)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='No limit';
        }

      if ( $user_role[0] == 'vendor' && $user_post_count2 > 1)
        {
            //set error message for my_field
            $errors['wpcf-omezeni']='If you want more job contact us';
        }

      if ( $user_role[0] == 'vendor' && $user_post_count > 999)
        {
            //set error message for my_field
            $errors['wpcf-omezeni']='No limit';
        }
    
    
        //check if featured image exists
       
    }
       
    
    //return result
    return array($fields,$errors);
}
#1184266

Hi Jakub,

Thank you for clarifying that.

From your message, I understand that you need to count "articles" for the form with ID "288" and "jobs" for the form with ID "375".

If that is correct, you can create two sperate functions for that, to avoid clutter.

For example, you can update your code for the "articles" form to:


add_filter('cred_form_validate','my_validation_article',10,2);
function my_validation_article($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']==288)

	{
		$user = get_current_user_id();
		$user_post_count = count_user_posts( $user , 'article' );
		$user_data = get_userdata($user);
		$user_role = $user_data->roles;

		if ( $user_role[0] == 'vendor' && $user_post_count > 3)
		{
			//set error message for my_field
			$errors['wpcf-omezeni']='If you want more article contact us';
		}

		if ( $user_role[0] == 'premium' && $user_post_count > 999)
		{
			//set error message for my_field
			$errors['wpcf-user-validation']='No limit';
		}

		//check if featured image exists

	}

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

And for the "jobs" form, you can add a new function as:


add_filter('cred_form_validate','my_validation_job',10,2);
function my_validation_job($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']==375)

	{
		$user = get_current_user_id();
		$user_post_count = count_user_posts( $user , 'job' );
		$user_data = get_userdata($user);
		$user_role = $user_data->roles;

		if ( $user_role[0] == 'vendor' && $user_post_count > 3)
		{
			//set error message for my_field
			$errors['wpcf-omezeni']='If you want more job contact us';
		}

		if ( $user_role[0] == 'premium' && $user_post_count > 999)
		{
			//set error message for my_field
			$errors['wpcf-user-validation']='No limit';
		}

		//check if featured image exists

	}

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

Please note how different function names, form IDs and validation messages are used for each code block.

regards,
Waqar

#1184367
Untitled.jpg

Hi Waqar,

i tried this setup last time (with 2 same code) and tried today with this result :-(:

Fatal error: Cannot redeclare my_validation() (previously declared in /web/htdocs4/ictportalcz/home/www/wp-content/themes/elementor-hello-theme-master/functions.php:59) in /web/htdocs4/ictportalcz/home/www/wp-content/themes/elementor-hello-theme-master/functions.php on line 137

Line 137 is last } in code (i write you this note into code below).

I check if is correct ID of form (get from Toolset->Post Forms) and name of form (get from Toolset-> Post Type -> Slug) in second part of code and it seems to be ok. You can find it in attachement.

Thanks

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']==288)
      
    {       
            $user = get_current_user_id();
            $user_post_count = count_user_posts( $user , 'clanek' );
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
              
        if ( $user_role[0] == 'vendor' && $user_post_count > 3)
        {
            //set error message for my_field
            $errors['wpcf-omezeni']='Je možné přidat pouze 4 články zdarma.';
        }
        
          if ( $user_role[0] == 'premium' && $user_post_count > 999)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Je možné zveřejnit pouze 999 článků.';
        }
    
    
        //check if featured image exists
       
    }
       
    
    //return result
    return array($fields,$errors);
}

 
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']==141)
      
    {       
            $user = get_current_user_id();
            $user_post_count = count_user_posts( $user , 'job' );
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
              
        if ( $user_role[0] == 'vendor' && $user_post_count > 3)
        {
            //set error message for my_field
            $errors['wpcf-omezeni']='Je možné přidat pouze 4 inzeárty zdarma.';
        }
        
          if ( $user_role[0] == 'premium' && $user_post_count > 999)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Je možné zveřejnit pouze 999 článků.';
        }
    
    
        //check if featured image exists
       
    }
       
    
    //return result
    return array($fields,$errors);
} // THIS IS #137 LINE IN MY FUNCTION.PHP
#1184835

Hi Jakub,

Your code is generating the "Fatal error" because, in PHP, it is not allowed to redeclare the same function name, more than once.

If you'll check your code, you'll note that "my_validation" name is being used to define both functions.
( screenshot: hidden link )

Please use unique function names for those functions, as shown in the example code in my last message ( screenshot: hidden link ) and you won't get that same error.

I hope this helps and please let me know how it goes.

regards,
Waqar

#1184888

My issue is resolved now. Thank you!