Title: How to do function call in wordpress
Last modified: October 25, 2018

---

# How to do function call in wordpress

 *  Resolved [Ash](https://wordpress.org/support/users/ash222/)
 * (@ash222)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-do-function-call-in-wordpress/)
 * Hello,
    I have a code in which particular user’s total balance is got.I am repeating
   this code several times. So I added the same code inside a function so that i
   can call that function when needed. The function takes user id as parameter. 
   Can u please guide me on how to call the function by passing parameter and return
   the result. If it is possible by using add_filter then how to do that?

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

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-do-function-call-in-wordpress/#post-10815924)
 * If it’s the currently logged in user, you can just use [https://codex.wordpress.org/Function_Reference/wp_get_current_user](https://codex.wordpress.org/Function_Reference/wp_get_current_user)
   or [https://developer.wordpress.org/reference/functions/get_current_user_id/](https://developer.wordpress.org/reference/functions/get_current_user_id/)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-do-function-call-in-wordpress/#post-10817657)
 * There’s no reason to use add_filter() unless you are modifying someone else’s
   code or you want someone else to modify your code. If your function needs the
   user ID as a parameter, you can call your function (called get_total_balance()
   here) like so:
 *     ```
       $id = get_current_user_id();
       $balance = get_total_balance( $id );
       echo 'Amount owed: ' . money_format('%i', $balance ) . "\n";
       ```
   
 * Your function declaration, minus the functional code within, would look something
   like this:
 *     ```
       function get_total_balance( $user_id ) {
         // take user ID and get their total balance
         // assign the balance to variable $bal
         return $bal;
       }
       ```
   
 *  Thread Starter [Ash](https://wordpress.org/support/users/ash222/)
 * (@ash222)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-do-function-call-in-wordpress/#post-10817711)
 * Hello,
    Thanks for the reply. I followed the same structure. I called the function
   $balance= wallet_balance($user_id); //here user_id is not of current logged in
   user. This function is used within the hook woocommerce_order_refunded.
 * function wallet_balance($user_id){
    //my code goes here }
 * But in this case i get error. user_id got is empty
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-do-function-call-in-wordpress/#post-10818995)
 * Show the code where you get the value you’re passing to the function.
 *  Thread Starter [Ash](https://wordpress.org/support/users/ash222/)
 * (@ash222)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-do-function-call-in-wordpress/#post-10821472)
 * hello,
    Sorry for disturbing. Found the answer. You are correct. Mistake I made
   was didn’t declare wpdb as global variable. global $wpdb;

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

The topic ‘How to do function call in wordpress’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [Ash](https://wordpress.org/support/users/ash222/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-do-function-call-in-wordpress/#post-10821472)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
