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.