How to trigger a function with a button?
-
Hi,
I have created a new endpoint in Woo My Account page with fields to fill out, but I have no idea how to create a button that would trigger the fields to be validated and saved? Additionally, on the same track, how can it be made that if it was already populated once, it would return the last results?
////***************************************//// ////********* Information content *********//// ////***************************************//// ////***************************************//// function instagram_get_account_fields() { return apply_filters( 'instagram_account_fields', array( 'user_instagram_username' => array( 'type' => 'text', 'label' => __( 'Instagram Username', 'instagram_username_field' ), 'placeholder' => __( 'Type your username here', 'instagram_username_field' ), 'required' => true, ), 'user_instagram_password' => array( 'type' => 'password', 'label' => __( 'Instagram Password', 'instagram_password_field' ), 'placeholder' => __( 'Type your password here', 'instagram_password_field' ), 'required' => true, ), 'user_instagram_whitelist' => array( 'type' => 'textarea', 'label' => __( 'Your whitelist', 'instagram_whitelist_field' ), 'placeholder' => __( 'username1, username2, username3 etc.', 'instagram_password_field' ), ), ) ); } function instagram_frontend_fields() { $fields = instagram_get_account_fields(); foreach ( $fields as $key => $field_args ) { woocommerce_form_field( $key, $field_args ); } } add_action( 'woocommerce_account_instagram_endpoint', 'instagram_frontend_fields'); ////***************************************//// ////*********** Check & validate **********//// ////***************************************//// ////***************************************//// add_action( 'woocommerce_save_account_details_errors','instagram_field_validation', 20, 1 ); function instagram_field_validation( $args ) { if ( isset($_POST['instagram_username_field']) && empty($_POST['instagram_username_field']) ) $args->add( 'error', __( 'Please type your Instagram username!', 'woocommerce' ),''); if ( isset($_POST['instagram_password_field']) && empty($_POST['instagram_password_field']) ) $args->add( 'error', __( 'Please type your Instagram password!', 'woocommerce' ),''); if ( isset($_POST['instagram_whitelist_field'])) $args->add( 'error', __( 'Please fill out your whitelisted profiles/accounts!', 'woocommerce' ),''); } ////***************************************//// ////********** Save the data if ***********//// ////******* validation successfull ********//// ////***************************************//// add_action( 'woocommerce_save_account_details', 'instagram_field_save', 20, 1 ); function instagram_field_save( $user_id ) { if( isset($_POST['instagram_username_field']) && ! empty($_POST['instagram_username_field']) ) update_user_meta( $user_id, 'instagram_username_field', sanitize_text_field($_POST['instagram_username_field']) ); if( isset($_POST['instagram_password_field']) && ! empty($_POST['instagram_password_field']) ) update_user_meta( $user_id, 'instagram_password_field', sanitize_text_field($_POST['instagram_password_field']) ); if( isset($_POST['instagram_whitelist_field']) && ! empty($_POST['instagram_whitelist_field']) ) update_user_meta( $user_id, 'instagram_whitelist_field', sanitize_text_field($_POST['instagram_whitelist_field']) ); }
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
The topic ‘How to trigger a function with a button?’ is closed to new replies.