Skip Navigation

[Résolu] cred_before_save_data & cred_form_validate Not Firing

This support ticket is created Il y a 4 années et 9 mois. 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.

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
- 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 9 réponses, has 2 voix.

Last updated by Dave Il y a 4 années et 9 mois.

Assisted by: Luo Yang.

Auteur
Publications
#1275309

I am trying to:

Use cred_before_save_data & cred_form_validate to perform various tasks when submitting a simple form from a modal window.

I expected to see:

The modal window closes, redirects to the correct page and processes the input data and saves processed output to the post.

Instead, I got:

The hooks are not firing.

I had all of this working no problem and then went on a painful mission to form custom validation, which is detailed here:

https://toolset.com/forums/topic/unable-to-get-custom-javascript-validation-to-work-with-cred-form/

All of the functionality worked. The hooks were working an the PHP was sound and delivering the desired results. However after testing once the JS validation had been completed I found that this code wasn't working and testing has lead me to conclude that the cred hooks aren't filing for some reason.

I will include the PHP here, but I have spent hours trying to pin down the problem, I have removed all of the custom JS I added to the validation, reset the form and views involved to their basic output, undoing all of my modifications, I have removed all of the code from the hooked functions and just dropped an echo and die(); in there, nothing. I do have WordPress debugging one but Views debugging is just causing never ending loads (that might be my laptop, I'll know tomorrow) and the only time I have seen the echo is when the page hung on a deliberate PHP error somewhere else. I honestly have no idea what could be causing this as I have changed nothing with these functions since getting them working originally. All of my work was on the cred form's JS and a little bit on the view's HTML.

Here is are the PHP functions:

/**  Create function to update the amount of the gift is purchased **/
function update_purchased_amount($form_data)
{
	//echo 'cred_before_save_data action hook is triggered!';
        //die();
	
	//Assign current gift post id to variable
	$id = get_the_ID();
	
	//update_post_meta( 92, 'wpcf-purchaser', '556677' );  //Testing and does not work
	
	//Check if there is an purchase amount entered and that it is not zero()
	if ( isset( $_POST['amount-purchased'] ) != 0 )	{
		
		//Load the existing amount of gifts claimed
		$bought = get_post_meta( $id, 'wpcf-quantity-claimed', true );
		
		//Load the amount of gifts purchased this time
		$new_bought = $_POST['amount-purchased'];
		
		//Add the amount of gifts purchased previously and now together
		$amount_bought = $bought + $new_bought;
		
		//Write the total amount of gifts purchased back to the database
		update_post_meta( $id, 'wpcf-quantity-claimed', $amount_bought );
		
		//Load the existing list of purchasers
		$purchasers = get_post_meta( $id, 'wpcf-purchaser', true );
		
		//Set the current user's id to the current purchasers
		$new_purchaser = get_current_user_id();
		
		//Force the amount bought into an integer and set it to the loop index number
		$index = (int) $new_bought;
		
		//If a gift has been purchased
		if ( $index > 0 ) {
			
			//Start a loop
			do {
				//Check if there are any existing purchasers
				if ($purchasers != 0 ) {
					//If there are then add a comma and the new purchaser to the list
					$purchasers = $purchasers . ", " . $new_purchaser;
				//If there are no existing purchasers
				} else if ( $purchasers == 0 ) {
					//Start the list with the current purchaser
					$purchasers = $new_purchaser;
				}
				//Reduce the index by one
				$index = $index - 1;
			//Repeat until the current purchaser has been logged the amount of times equivalent to the number of this gift they bought
			} while ( $index > 0 );
		}
		
		//Write the updated purchasers list back to the database
		update_post_meta( $id, 'wpcf-purchaser', $purchasers );
	}
}

//Add function call for when number of purchases form is submitted
add_action('cred_before_save_data_139', 'update_purchased_amount', 10, 1);
/**  Create function to redirect back to the correct point on the gift list after a gift is purchased **/
function gift_purchase_redirect() {
	
	//echo 'cred_success_redirect action hook is triggered!';
        //die();
	
	//Assign current gift post id to variable
	$id = get_the_ID();
	
	//$list = isset( $_POST['amount-purchased'] );
	
	$parent = toolset_get_related_post( $id, 'gift-list-gift', 'parent' );
	
	$url = get_page_link( 54 ) . "?fw-gift-list=" . $parent . "#gifts_anchor";
	
	return $url;

}

//Add function call to redirect when number of purchases form is submitted
add_action('cred_success_redirect_139', 'gift_purchase_redirect', 10, 1);

I want to reiterate that these were working perfectly with the structure I had in place, yet now will not fire as far as I can tell.

I'm not sure if there is any benefit to posting all of the view and cred output on here but I can provide it if needed and the JS I've written is available in the other support thread I linked above, but I have removed it and it has made no difference.

I'm not asking for help writing custom code, just trying to figure out why these hooks have stopped working.

#1275397

Hello,

I have tried both hooks in my localhost with a fresh wordpress installation + the latest version of Toolset Forms plugin, all work fine, please check these:
1) Disable the AJAX feature of your post form
2) Test the PHP codes like this:
cred_before_save_data

add_action('cred_before_save_data_139', function($form_data){
    //some code here
	die('cred_before_save_data is triggered');
});

cred_form_validate:

add_filter('cred_form_validate', function( $data, $form_data ) {
	if ($form_data['id']==139){
		die('cred_form_validate is triggered');
	}
    return $data;
}, 10, 2);
#1275573
No AJAX.png

Hi Luo,

There is no AJAX running on the form as shown in the screenshot, and I said in the original post that I have tried echo and die functions to no avail. However, your approach is slightly different so I tried it anyway and had the exact same results, cred_before_save_data and cred_success_redirect did not cause the die() to trigger, cred_form_validate did, but that is not the issue I am having, that is a mistake on my part.

I do need to apologise for a bit of confusion though, I incorrectly labelled the ticket with cred_before_save_data and cred_form_validate, which is incorrect. It was late and I'd been trying to fix this for hours so I made a mistake, so I do apologise for that. However, as per the code I posted, the problematic hooks are cred_before_save_data and cred_success_redirect. But as I said the code I posted was correct.

To reiterate, this was all working perfectly in the setup I have, so I am at a loss as to what is interfering. I don't think I've added or updated any plugins but I am going to have a look at that now.

Also, I am not accusing the hooks of not working, but as I stated, I am seeking help in figuring out why they have stopped working in this instance.

I'm still at a total loss as to why these two hooks aren't firing.

#1275587

The problem you mentioned above is abnormal, please check these:
1) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2019, deactivate/remove all other custom PHP/JS code snippets, and test again

2) Also check if there is any PHP/JS error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/

3) If the problem still persists, please provide database dump file(ZIP file) of your website, also point out the problem page URL and form URL, I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/

#1276193

I've been pulling the site apart trying to pin this error down and I have discovered that it is not related to plugins or themes or anything like that.

I have made a new number submission form and just made left it as simple as possible and the hooks fired.

There is something in the cred form itself that is stopping them from firing. I'll keep working to try and figure it out and if I do get there before you I'll make sure to post it here to save you the trouble.

#1276201

I haven't managed to solve this yet, but I have narrowed it down to the lines of code involved.

It does have something to do with the JS, but not exclusively.

My testing has shown that removing the JS does not solve the error, however removing these two lines from the cred form do cause the error to disappear and the hooks to run normally:

<span style="display: none;">[cred_field field="gift-quantity" force_type="field" class="form-control" output="bootstrap" value="[types field='gift-quantity' output='raw'][/types]"]</span>
      <span style="display: none;">[cred_field field="quantity-claimed" force_type="field" class="form-control" output="bootstrap" value="[types field='quantity-claimed' output='raw'][/types]"]</span>

If I leave these lines in and remove the JS then it appears that the redirect fires but the before_save hook doesn't as the numbers don't update.

I will keep experimenting with this tomorrow but I hope that this might give you some clues as to where to look for the issue as it does seem that the Toolset hooks are reacting in a weird way to what appears to be just normal HTML.

#1276245

Thanks for the details, I am downloading the files, will update here if there is anything found.

#1276347

Thanks for the details, I can install your duplicator package in my localhost,
I assume we are talking about the post form "Number of Gifts Purchased"(ID 139).

here are what I found, there should be some problem in your theme file fw-functions.php.

I have tried these, edit the theme file functions.php, remove this line:

include_once dirname(__FILE__) . '/fw-functions.php';

And add below codes:

add_action('cred_before_save_data_139', function($form_data){
    //some code here
    die('cred_before_save_data is triggered');
});

Test the post form "Number of Gifts Purchased" in front-end, after submit the form, I can see the message:
cred_before_save_data is triggered

Same as above, if I use below codes:

add_action('cred_success_redirect_139', function($url) {
     //some code here
    die('cred_success_redirect is triggered');
});

After submit the form, I can see the message: cred_success_redirect is triggered

So this problem is in custom codes of your theme file fw-functions.php, I suggest you debug them line by line, for example, remove all functions, and add them one by one, test and locate the problem codes.

#1276577

I don't mean to appear rude Luo, but fw-functions.php contains every custom function for this website as it is a development site for future projects and modules. You have essentially taken a complete copy of the site and simply said that there is a problem with my functions file.

I have already stated the specific functions and areas of code that are a problem and simply pointing to the file containing the functions is not remotely helpful.

I clearly stated in the original post that I am seeking help specifically debugging this particular issue where something is preventing your hooks from firing and of course it is going to be related to the theme functions file.

I apologise if there has been some kind of misunderstanding, but if you are unable to help me then please simply say so rather than providing a generic answer that does not aid in solving the problem.

#1276687

I have finally figured it out, for whatever reason having the JS validation in place means that I had to use the cred_save_data hook rather than the cred_before_save_data hook.

This was 100% working before I put the validation in but after stepping through every single element it turns out that the code was working perfectly within the hook but once the function had completed it was then resetting the data despite actually writing back to the database. I have confirmed this by managing to dump out the value from the database using the get_post_meta function just before the closing } of the hook function and yet after that it is gone.

I have no idea why this is happening and honestly it feels like it might be a bug in the hook execution priorities or something, but I have it working so I will close the ticket and carry on.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.