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
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
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 !!!
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
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?
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
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
Thanks for the details, I am downloading the files, will feedback if there is anything found
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
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)
I suggest you use some CSS codes to hide it, for example:
input[name='wpcf-user-validation']{
display: none !important;
}
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?
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
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;
}
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.