Problem: I would like to use cred_before_save_data to prevent a User from submitting the same form multiple times to create multiple posts, or to edit the same post more than once.
Solution: The cred_before_save_data hook should not be used to validate form submissions. Instead, use the cred_form_validate hook. This will allow you to return meaningful error messages to the front-end of the site. Here is an example showing how to prevent a User from creating more than one post, or how to prevent a User from editing a post where a specific custom field already has a value:
add_filter('cred_form_validate','profile_exists_validation',10,2); function profile_exists_validation($error_fields, $form_data) { global $current_user; //field data are field values and errors list($fields,$errors)=$error_fields; //uncomment this if you want to print the field values //print_r($fields); //validate if specific form if ($form_data['id']==12345) { //check custom field value if (isset($fields['wpcf-sel1']['value']) && $fields['wpcf-sel1']['value']!='') { //set error message for my_field $errors['wpcf-sel1']='This custom field has already been defined for the post.'; } //check if current User is already the author of a "Profile" custom post $args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' => 1, 'post_type' => 'profile' ); $profiles = get_posts( $args ); if ( sizeof($profiles) > 0 ) { //set error message for post title $errors['post_title'] = 'Profile already exists, so you cannot create another one.'; } } //return result return array($fields,$errors); }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
https://codex.wordpress.org/Template_Tags/get_posts
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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 4 replies, has 2 voices.
Last updated by 6 years, 4 months ago.
Assisted by: Christian Cox.