Skip Navigation

[Gelöst] cred conditional submission

This support ticket is created vor 7 Jahren, 1 Monat. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 9 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Kostas vor 7 Jahren, 1 Monat.

Assistiert von: Luo Yang.

Author
Artikel
#578449

Hi,

On a membership site I am using a cred form to create new content of answers CPT (which are children of samples CPT).
I use 'cred_save_data' hook to set custom title, slug for the CPT. Till this point all works fine.
Now I need to add this condition: On submit, check if there is already an answer with the same title, then print an error message and abort submission (do not create new answer). If there is no answer with the same title, then proceed normally and create new answer.
I am using the following code:

/*
 * Create a dynamic answer title/slug by the CRED form.
 */
function custom_save_data_action($post_id, $form_data) {
    if ($form_data['id']==171) {
	// find parent sample
        $parent_id = get_post_meta($post_id, '_wpcf_belongs_sample_id', true);
        $post_sample = get_post($parent_id);

        //Find current user
        $current_user = wp_get_current_user();

        $answer_slug = $post_sample->post_name . '-' . $current_user->user_login;
        $answer_title = 'Answer for ' . $post_sample->post_title . ' from ' . $current_user->user_firstname . ' ' . $current_user->user_lastname;

		// check if answer already exists
		$answer_exists = get_page_by_title($answer_title, OBJECT, 'answer');
		if (isset($answer_exists)) {
			// $message = '<br />Answer for this sample already sumbitted';
			echo '<br />Answer for this sample already sumbitted';
			// return $message;
			exit;
		}
		else {     
        	       //collect data and define new title/slug
        	       $my_post = array (
        	           'ID' => $post_id,
            	           'post_title' => $answer_title,
            	           'post_name'  => $answer_slug,  
        	       );
                
        	      // Update the post into the database
        	      wp_update_post( $my_post );
		}
	}
}
add_action('cred_save_data', 'custom_save_data_action', 10, 2);

Above code, if answer already exists, prints the message in a white page (no WP theme) AND creates a new answer with CRED Auto Draft title. I need the message in the WP theme and no answer created.

Any ideas/guidance is welcome.

Thank you,
Kostas

#578453

Dear Kostas,

You an try with CRED filter hook "cred_form_validate", when user submit the CRED form, use filter hook "cred_form_validate" to trigger a PHP function, in this PHP function do these:
1) Get the post title user submitted.
2) find those post with same post title
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
title (string) - use post title (available with Version 4.4).
If there is existed result, then return a error message

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
cred_form_validate
This hook provides custom validation for form fields

#578464

Hi Luo,

Thank you for your guidance.

The problem is that the answer post title is not submitted by the user. It is created during the form submission (shown in the code above) and post_id argument is needed for that.
So I do not see how I can create answer post title using the cred_form_validate hook you suggest.

Any more ideas please?

Kostas

#578884

In your case, you are going to do the validation before the answers post save into database, so there isn't any "answers" post id. I checked you codes, to setup the child post title, you need the parent "sample" ID, there should be a "_wpcf_belongs_sample_id" field in the CRED form, when user submit the CRED form, you can get the parent "sample" ID for PHP global variable $_POST, for example:

...
$parent_id = $_POST['_wpcf_belongs_sample_id'];
$post_sample = get_post($parent_id);
...
#579164

Hello Luo,

Thank you for the help.
I am now using cred_form_validate (as shown below), but I see no error messages, nor the answers are stopped from being created.

add_filter('cred_form_validate', 'custom_check_answer_exists', 10, 2);
function custom_check_answer_exists($error_fields, $form_data) {
	if ($form_data['id']==171){
               . . . 
               $sample_id = $_POST['newparent'];

		// field data are field values and errors
    	        list($fields, $errors) = $error_fields;
		$fields['newparent']['value'] = $sample_id;

		if (in_array($sample_id, $answered_sample_list, true)) {
                         $errors['newparent']='Already submitted!';
		}
		return array($fields, $errors);
        }
}

Any idea what I am doing wrong?

Thanks,
Kostas

#579225

This is a custom codes question, please provide a test site with the same problem, and fill below private detail box with login details, also point out the problem page URL and CRED form URL, I need to test and debug it in a live website. thanks

#579271

Thanks for the details, I can login your website, Please provide the FTP access in your hidden message:
https://toolset.com/forums/topic/cred-conditional-submission/#post-579258

also point out where I can edit your PHP codes

#579275

Hi Luo,

I do not have ftp access.
You can make changes and edit functions.php through the backend.

Thanks,
Kostas

#579315

Here is what I found, in the CRED form "Create content - Answers ":
1) you are using below custom shortcode to display the field "newparent",
[kkutraki_new_sample_select]
it is not a CRED shortcode, which is out the range of Toolset support, I suggest you ask help from the shortcode author to check if it is possible to use the custom shortcode [kkutraki_new_sample_select] to display the error message

2) And in the problem page:
versteckter Link
The shortcode [kkutraki_new_sample_select] does not display any options in the dropdown field, so it is not possible to test it in your website

3) and I setup a demo to use filter hook "cred_form_validate" in your website:
a) Edit the CRED form "Create content - Answers", line 7, modify the codes from:

[kkutraki_new_sample_select]

To:


		[kkutraki_new_sample_select-1]
      <select name="newparent1" class="form-control" output="bootstrap" required="" id="8200">
<option value="650">Test1</option>
</select>
      [cred_generic_field field='display_msg' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"0"
}
[/cred_generic_field]

It will output a dropdown menu field "newparent1" and a hidden generic field "display_msg"

3) Since you did not provide FTP access, so I use "code snippet" plugin to add below PHP codes:
versteckter Link

add_filter('cred_form_validate', 'custom_check_answer_exists', 10, 2);
function custom_check_answer_exists($error_fields, $form_data) {
	list($fields,$errors) = $error_fields;
    if ($form_data['id']==171){
	  	$sample_id = $_POST['newparent1'];
	  	$answered_sample_list = array(650);
        if (in_array($sample_id, $answered_sample_list)) {
		  	$errors['display_msg']='Already submitted!';
		}
	}
  	return array($fields,$errors);
}

It will be able to check if "newparent1" field is value 650, then output the error message.
You can test the result here:
versteckter Link

#579673

Thank you Luo.
That hidden cred generic field did the trick 🙂

Cheers,
Kostas