Skip Navigation

[Resolved] limit the number of posts that users can write

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
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 community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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)

Tagged: 

This topic contains 16 replies, has 2 voices.

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

Assisted by: Luo Yang.

Author
Posts
#607288

how can I limit the number of post types that registered user can write.
the limit is 3

I would like to avoid using additional plugins

thanks

#607433

Dear Massimo,

There isn't such a built-in feature within Toolset plugin, it needs some custom codes, for example you can try this:
1) create a CRED form for creating post
2) when user submit the CRED form, use filter hook cred_form_validate to trigger an PHP function, count the user's post, if it is out the limitation, then return an error,

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

See similar thread:
https://toolset.com/forums/topic/limiting-cred-form-submission-to-one-post/#post-477531

#608973

Hello,
I followed your guide, I entered this code:

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']==348)
    {       
            $user = get_current_user_id();
            $user_post_count = count_user_posts( $user , 'house' );
            $user_data = get_userdata($user);
            $user_role = $user_data->roles;
             
        if ( $user_role[0] == 'subscriber' && $user_post_count > 2)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Puoi inserire 3 Creature';
        }
   
              //check if featured image exists
      
    }
   
    //return result
    return array($fields,$errors);
}

I entered the id of my cred module and entered "subscriber".

I then entered the "user-validation" field in the cred module.

[cred_field field='user-validation' post='creatura' value='' urlparam='' readonly='true' class='form-control' output='bootstrap']

does not work !!!

#609164

Since it is a custom PHP codes problem, please provide a test site with same problem, and fill below private detail box with login details and FTP access, also point out the problem page URL and CRED form URL, and where I can edit you PHP codes, I need a live website to test and debug, thanks

#609339

I tried the FTP access you provided above, but get this error:
Status: Logged in
Status: Retrieving directory listing...
Command: PWD
Response: 257 "/" is your current location
Command: TYPE I
Response: 200 TYPE is now 8-bit binary
Command: EPSV
Response: 500 Unknown command
Error: Failed to retrieve directory listing

Is there any special FTP setting required?

#609398

FTP & explicit FTPS port: 21

I tried Ftp, it works with the parameters I gave. let me know if you can not get in. thank you

#609609

No, I tried it again, it does not work, same problem:
Status: Logged in
Status: Retrieving directory listing...
Command: PWD
Response: 257 "/" is your current location
Command: TYPE I
Response: 200 TYPE is now 8-bit binary
Command: EPSV
Response: 500 Unknown command
Error: Failed to retrieve directory listing

So I can not edit your PHP codes, please provide a copy of your website:
https://toolset.com/faq/provide-supporters-copy-site/
You can put it into your own google drive disk, and share the link in the "private detail box", I need to duplicate same problem and debug it in my localhost, thanks

#610025

Thanks for the details, I am downloading the files, will feedback if there is anything found

#610032

Here are what I found, please try these:
1) Dashboard-> Toolset-> Post fields
Edit a the group "gruppo campi creature", and add a custom numeric field "user-validation"

2) Dashboard-> Toolset-> Post forms
Edit form "aggiungi creatura", put above custom field into the form content:

[cred_field field='user-validation' post='creatura' value='' urlparam='' readonly='true' class='form-control' output='bootstrap']

3) Edit your PHP codes to below:

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']==348)
    {       
		$user = get_current_user_id();
		$user_post_count = count_user_posts( $user , 'creatura' ); // creatura is the  custom post type slug
		$user_data = get_userdata($user);
		$user_role = $user_data->roles;
        if ( $user_role[0] == 'subscriber' && $user_post_count > 2)
        {
            //set error message for my_field
            $errors['wpcf-user-validation']='Puoi inserire 3 Creature';
        }
              //check if featured image exists
    }
    
    //return result
    return array($fields,$errors);
}

Then test again

#610089
esempio35.jpg

it works !!!

to hide the field "user validation" , I added the hide tag (see code) to the class.

[cred_field field='user-validation' post='creatura' value='' urlparam='' readonly='true' class='form-control hide' output='bootstrap']

Is it correct to do so?

I would like to remove o modify the error message? (see screenshot)

#610095

I suggest you use some CSS codes to hide it, for example:

input[name='wpcf-user-validation']{
display: none !important;
}
#610150
esempio36.jpg

ok I inserted the CSS and it works.
in the error message when you exceed the number of posts also appears in the name of the field "user validation"
How can I remove the field name from the error message?

#610546

That is the filed name of custom field "user validation", you can change it to what you want, see the step 1) I mentioned in post:
https://toolset.com/forums/topic/limit-the-number-of-posts-that-users-can-write/#post-610032

#610612
esempio35.jpg

Hello,
I do not want to change the name of the field.
as I wrote, I want to remove the name from the error message
(see photo)

I tried with this CSS, but it does not work

.alert alert-danger {
display: none !important;
}
#610620

There isn't such a built-in feature within CRED plugin, and if you use CSS codes to hide the it, it will mess all messages, if you agree, we can take it as a feature request, our developers will evaluate it.

The forum ‘Types Community Support’ is closed to new topics and replies.