Check your PHP error log.
Hi! Takayuki, thanks for your excellent work with Contact Form 7, it’s a tremendous helper in my jobs.
Here’s the error logged:
[Fatal error] => 09.11.2016 16:47:20
Cannot use object of type WPCF7_Shortcode as array
in /home/mcmovil/public_html/wp-content/themes/mcmovil-theme/functions.php (209)
The “line 209” is this:
if ( $tag['name'] == 'user-plan' ) {
Cheers!
-
This reply was modified 9 years, 6 months ago by
maehdros.
Yes, you are using a WPCF7_Shortcode object ($tag) as an array there. You can’t do that.
Got it! That bug is solved. Here’s the code:
add_filter( 'wpcf7_validate_checkbox*', 'user_plan_validation_filter', 10, 2 );
function user_plan_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( $tag->name == 'user-plan' ) {
$user_plan = $_POST['user-plan'];
// $user_plan = isset( $_POST['user-plan'] ) ? trim( $_POST['user-plan'] ) : '';
if ( $user_plan == '' ) {
$result->invalidate( $tag, "Selecciona un plan para poder finalizar el tramite" );
}
}
return $result;
}
Now the validation process doesn’t get stuck, but the validation message shown is not my custom one but the one set in the form options. What can I be doing wrong?
Thanks a lot for your help Takayuki, cheers!
Some debug information I pulled out from “debug.log”:
[10-Nov-2016 17:09:58 UTC] PHP Notice: Undefined index: user-plan in /home/mcmovil/public_html/wp-content/themes/mcmovil-theme/functions.php on line 212
Line 212 is this:
$user_plan = $_POST['user-plan'];
Sorry for the commented line in the previous post code, I forgot to clean it.
Well, at this point, I’ve arrived to a code that is working without bugs, but it keeps failing in its purpose:
add_filter( 'wpcf7_validate_checkbox*', 'user_plan_validation_filter', 20, 2 );
function user_plan_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
$field = 'user-plan';
if ( $field == $tag->name ):
if ( isset( $_POST['user-plan'] ) ):
$user_plan = trim( $_POST['user-plan'] );
else:
$user_plan = '';
endif;
if ( $user_plan == '' ):
$result->invalidate( $tag, "Selecciona un plan para poder finalizar el tramite" );
endif;
endif;
return $result;
}
Using the PHP Error Log I checked that all the variables are being filled with the values I expect, the conditional sentences also are working as expected, but the validation message keeps showing the one setted in the Contact Form configuration.
Any help?
Thanks!
Hi maehdros,
I exactly have the same problem as you, i.e., my filter is working fine but the validation message keeps showing the one set as default and not my custom one. Did you find any solution for this?
Thanks a lot!
Nima