Skip Navigation

[Resolved] Trying to limit CRED from post content to 500 words or less

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

Problem:

I add a snippet to limit the number of words for post content in the form and I get this error:

Cannot redeclare function my_validation.

Solution:

Change the name of the function as you used that somewhere else on your WordPress installation:

add_filter('cred_form_validate','chr_validation',10,2);
function chr_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;
    
    //check if post_title field is more than 500 words
    if ( str_word_count($fields['post_content']['value']) > 500 )
    {
        //set error message for post_content field
        $errors['post_content']='500 word count exceeded. Please, use less words for your description.';
    }   
   
    //return result
    return array($fields,$errors);
}

Relevant Documentation:

https://toolset.com/forums/topic/limit-word-count-on-cred-post_content-field/

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.

This topic contains 2 replies, has 2 voices.

Last updated by larryB-3 1 year, 5 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2614339

I was following the code listed here: https://toolset.com/forums/topic/limit-word-count-on-cred-post_content-field/

But I got an error that Snippet automatically deactivated due to an error on line 3:
Cannot redeclare function my_validation.

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;

//check if post_title field is more than 500 words
if ( str_word_count($fields['post_content']['value']) > 500 )
{
//set error message for post_content field
$errors['post_content']='500 word count exceeded. Please, use less words for your description.';
}

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

Also is this code checking the post_title field or the post content field? I am really just trying to limit the number of words a front-end user can put in the post content field when submitting a post form.

#2614587

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

The code that you shared is about title but if you want to have for the content here is the code:

add_filter('cred_form_validate','chr_validation',10,2);
function chr_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;
   
    //check if post_title field is more than 500 words
    if ( str_word_count($fields['post_content']['value']) > 500 )
    {
        //set error message for post_content field
        $errors['post_content']='500 word count exceeded. Please, use less words for your description.';
    }   
  
    //return result
    return array($fields,$errors);
}

The reason that you get the error you have mentioned is that somewhere else in your code you declared a function name in PHP with the same name.

In the code above, I changed the name to chr_validation to have a unique name that you have never used before.

Thanks.

#2614785

My issue is resolved now. Thank you!