Title: How to trigger a function with a button?
Last modified: October 23, 2019

---

# How to trigger a function with a button?

 *  Resolved [showtek](https://wordpress.org/support/users/showtek/)
 * (@showtek)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/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)

 *  [3 Sons Development](https://wordpress.org/support/users/3sonsdevelopment/)
 * (@3sonsdevelopment)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/how-to-trigger-a-function-with-a-button/#post-12058106)
 * Hey [@showtek](https://wordpress.org/support/users/showtek/),
 * That’s a great question. I would take a look at a tutorial like this one that
   can show you how to add custom fields to WooCommerce customer accounts:
 * [https://iconicwp.com/blog/the-ultimate-guide-to-adding-custom-woocommerce-user-account-fields/](https://iconicwp.com/blog/the-ultimate-guide-to-adding-custom-woocommerce-user-account-fields/)
 * It covers more than adding them only to an account endpoint, but that is in there
   as well. See if that won’t give you some direction as you work on this.
 * Take care
 *  Thread Starter [showtek](https://wordpress.org/support/users/showtek/)
 * (@showtek)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/how-to-trigger-a-function-with-a-button/#post-12058175)
 * Hey [@3sonsdevelopment](https://wordpress.org/support/users/3sonsdevelopment/),
 * Thank you very much for your reply! I have previously reviewed the detailed tutorial
   and based my current code from them, however, it only stated the custom fields
   to be validated and saved in either check-out fields or my account fields where
   a button is already located. While it may solve a problem by migrating my code
   to that section, it may not follow the ease of use for the website I am trying
   to build 🙁
 *  [con](https://wordpress.org/support/users/conschneider/)
 * (@conschneider)
 * Engineer
 * [6 years, 7 months ago](https://wordpress.org/support/topic/how-to-trigger-a-function-with-a-button/#post-12098324)
 * Hi there,
 * > While it may solve a problem by migrating my code to that section, it may not
   follow the ease of use for the website I am trying to build 🙁
 * Did you make it work?
 * Kind regards,
 *  [con](https://wordpress.org/support/users/conschneider/)
 * (@conschneider)
 * Engineer
 * [6 years, 7 months ago](https://wordpress.org/support/topic/how-to-trigger-a-function-with-a-button/#post-12122370)
 * Hi there,
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   if you have any further questions, you can start a new thread.
 * Kind regards,

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to trigger a function with a button?’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [button](https://wordpress.org/support/topic-tag/button/)
 * [forms](https://wordpress.org/support/topic-tag/forms/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [my account](https://wordpress.org/support/topic-tag/my-account/)
 * [save](https://wordpress.org/support/topic-tag/save/)
 * [validation](https://wordpress.org/support/topic-tag/validation/)

 * 4 replies
 * 3 participants
 * Last reply from: [con](https://wordpress.org/support/users/conschneider/)
 * Last activity: [6 years, 7 months ago](https://wordpress.org/support/topic/how-to-trigger-a-function-with-a-button/#post-12122370)
 * Status: resolved