UM()->form()->add_error( ) not working
-
Hello Champ Camba! I using Ultimate Member plugin, this is the best membership plugin, thanks for the Ultimate Member team.
I want to add the google recaptcha v2 invisible captcha to the default login form, and this this my code:
<?php //add recaptcha invisible script to UM login form add_action( 'um_after_form', array($this, 'my_recaptcha_invisible_script'), 10, 1); function my_recaptcha_invisible_script($args){ $mode = $args['mode']; if( $mode == 'login' ){ $html = ''; ob_start(); ?> <script> //add a class to the form element var addFormClass = document.querySelectorAll(".um-login form"); addFormClass[0].classList.add("um-login-recaptcha-invisible"); </script> <script src="https://www.google.com/recaptcha/api.js" async defer></script> <script> function onSubmit(token) { document.getElementsByClassName("um-login-recaptcha-invisible")[0].submit(); } </script> <script> //add site key and class to the form submit button var Inputs = document.querySelectorAll(".um-login-recaptcha-invisible input"); for( var i = 0; i < Inputs.length; i++ ) { if( Inputs[i].type.toLowerCase() == 'submit' ) { Inputs[i].classList.add('g-recaptcha'); Inputs[i].setAttribute('data-sitekey', 'xxxxxx'); Inputs[i].setAttribute('data-callback', 'onSubmit'); } } </script> <input type="hidden" name="my_recaptcha_um_error" id="my_recaptcha_um_error" value="true" data-key="my_recaptcha_um_error" /> <?php $html = ob_get_contents(); ob_end_clean(); echo $html; //Show error msg if ( $mode == 'login' && UM()->fields()->is_error( 'my_recaptcha_um_error' ) ) { echo UM()->fields()->field_error( UM()->fields()->show_error( 'my_recaptcha_um_error' ) ); } } } //Verify user submit add_action( 'um_submit_form_errors_hook_login', array($this, 'my_recaptcha_um_login_verification'), 10, 1 ); function my_recaptcha_um_login_verification(){ $mode = $args['mode']; if( $mode == 'login'){ $response = isset( $_POST['g-recaptcha-response'] ) && !empty( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( $_POST['g-recaptcha-response'] ) : ''; $v2_invisible_secretkey = 'xxxx'; $remoteip = esc_textarea( $_SERVER['REMOTE_ADDR'] ); $verify_url = 'https://www.recaptcha.net/recaptcha/api/siteverify'; $args_array = $verify_url . '?secret=' . $v2_invisible_secretkey . '&response=' . $response . '&remoteip=' . $remoteip; $response_json = wp_remote_post( $args_array ); $obj_json = wp_remote_retrieve_body( $response_json ); $result = json_decode($obj_json, true); //I use print_r() to print the result and google return the json normally echo '<pre>'; print_r($result); echo '</pre>'; /* print_r result Array ( [success] => 1 [challenge_ts] => 2022-01-04T02:26:54Z [hostname] => 192.168.9.168 ) */ if( isset($result['success']) && $result['success'] == "1" ){ //if success nothing need to do }else{ //add this error since not work UM()->form()->add_error( 'my_recaptcha_um_error', __('ERROR: Captcha verification failed, please try again!!!') ); } //even I change the code like this , when user click submit , still login success, not show the error msg and not stop the user to login if( isset($result['success']) && $result['success'] == "1" ){ } //This error still not work UM()->form()->add_error( 'my_recaptcha_um_error', __('ERROR: Captcha verification failed, please try again!!!') ); } } }
Could you tell me where is the problem ?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘UM()->form()->add_error( ) not working’ is closed to new replies.