Skip Navigation

[Resolved] cred_form_validate runs 2x and toolset_connect_posts creates double connections

This support ticket is created 6 years, 4 months ago. 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.

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)

This topic contains 11 replies, has 4 voices.

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

Assisted by: Luo Yang.

Author
Posts
#1077186
Screen Shot 2018-08-08 at 15.24.39.png
Screen Shot 2018-08-08 at 15.19.15.png

I am making an event registration page (see screenshot).
I have CPT Event and Candidate with a many-to-many relationship.

I have made an event registration form using a CRED User Form.
- New users get a Candidate post and are registered for the Event using "cred_submit_complete".
- Existing users are registered for the Event using "cred_form_validate".

Registration is done with "toolset_connect_posts".

I'm using Code Snippets (https://wordpress.org/plugins/code-snippets/) to create PHP snippets.

add_action('cred_form_validate', 'gwf_register_validate', 10, 2);
function gwf_register_validate($errors, $form_data) {
  
  if( isset($_GET['event_id']) && $form_data['id'] === 1689 ) {
	
	// Search users by email
	$user_email = $errors[0]['user_email']['value'];
	$users = new WP_User_Query( 
	  array( 'search' => $user_email )
	);
	$users_found = $users->get_results();
	
	if ( count($users_found) > 0 ) {
	  $user_id = $users_found[0]->ID;
	  
	  // Search candidate ID of user
	  $args = array( 'post_type' => 'candidate', 'author' => $user_id, 'posts_per_page' => 1 );
	  $posts = get_posts($args);
	  $candidate_id = $posts[0]->ID;
	  
	  // Connect candidate and event
	  $event_id = $_GET['event_id'];
	  toolset_connect_posts( 'candidate-event', $candidate_id, $event_id );
	}
  }
}

The first problem is that 'cred_form_validate' runs twice. This by itself is not a problem.
(Note: there is only 1 form on the page)

The main problem is that 'toolset_connect_posts' also runs twice and two identical connections are made.
For example: Candidate A gets two identical connections to Event B. (see the screenshot)

If I'm not mistaken then 'toolset_connect_posts' is not supposed to make double connections.

#1077287

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I don't think the cred_form_validate filter runs twice, so I set up a simple test to confirm.

I created a New User form and added the following code to my functions.php:

function tssupp_test_filter( $errors, $form_data ){

 	$now = microtime();

 	error_log("Filter triggered at " . $now );

}
add_filter( 'cred_form_validate', 'tssupp_test_filter', 10, 2 );

This is what appeared in my debug.log:

[08-Aug-2018 08:42:59 UTC] Filter triggered at 0.13696900 1533717779

So, confirmed, the filter only runs once.

If it is running twice on your site it must be something to do with your workflow or the custom code you added.

If toolset_connect_posts is duplicating the connection it must be because it is being called twice.

I'm not sure I understand the logic of
- New users get a Candidate post and are registered for the Event using "cred_submit_complete".
- Existing users are registered for the Event using "cred_form_validate".

Are you talking about separate forms for each scenario or are you trying to combine the functionality into a single form (which means *both* the cred_form_valiate and cred_submit_complete hooks will be triggered, which may account for your code running twice).

Anyway, you will need to revise your workflow to understand why you have code running twice.

#1078282

Nigel, thank you for your support!

1. There are no problems with new users if both "cred_form_valiate" and "cred_submit_complete" are enabled. In my code above you can see that "toolset_connect_posts" in "cred_form_valiate" only runs if a user exists.
The problem is when an existing user registers for the event, then "toolset_connect_posts" in "cred_form_valiate" only fires.

2. I have disabled all plugins except for Toolset and Code Snippets.
And I have disabled all code snippets except for the one below

add_action('cred_form_validate', 'gwf_test', 10, 2);
function gwf_test($errors, $form_data) {
  print_r("print test!!!!");
}

It still runs twice.

What else can I try?

#1078291

I have found another problem when I try to check if the connection exists between the candidate and the event in "cred_form_validate" as below:

	  // Check if candidate already has event connection
	  $events = toolset_get_related_posts(
		$candidate_id,
		'candidate-event',
		'parent'
	  );
	  
	  if ( !in_array($event_id, $events) ) {
		// Connect candidate and event if the connection doesn't exists
		toolset_connect_posts( 'candidate-event', $candidate_id, $event_id );
	  }

The second time that "cred_form_validate" runs, there still isn't a connection that was made in the first run.
But after "cred_form_valiate" has finished there are two connections after all!

#1079491

I tried "cred_form_validate" on another website and there is no problem on that website.

On the website with the problem I disabled all other plugins except for Toolset Forms. Furthermore I changed the theme to twentyseventeen and I added "cred_form_validate" to functions.php instead of using the Code Snippets plugin. It still runs twice.

#1079644

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Nigel is on vacation. This is Minesh here and I'll take carer of this ticket and try to help you further. Hope this is OK.

Well - I need problem URL and access details to check why the hook cred_form_validate run two time.

Please let me know where exactly you added the hook: cred_form_validate

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1081395

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I try to connect to SFTP but I could not able to.

Could you please send me working FTP/SFTP access details as well as let me know if any special port number I need to use.

I have set the next reply to private which means only you and I have access to it.

#1083055

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - I see the issue on your site. We need duplicator copy of your site to debug this issue further.

Could you please share 1:1 copy of your site so that we can report to next level support.
=> https://toolset.com/faq/provide-supporters-copy-site/

I have set the next reply to private which means only you and I have access to it.

#1084473

Hello,

Minesh isn't available, I will take care this ticket, and I am downloading the files, will update here if there is anything found.

#1084505

I can duplicate the same problem, and it is a known problem, it has already been put in our to-do list, our developers are working on it, I will update this thread if there is anything news.

#1239632

Here is the update:
It is fixed in future version of Toolset form plugin 2.4, it is in QA status, will be released soon.

#1278121

Hello,

Toolset Form version 2.4 is released, you can download it here:
https://toolset.com/account/downloads/

please test and check if it is fixed, thanks