I am trying to display information from a custom table from my wordpress users pages

to preface this I know my way around a computer but I'm no coder by any means. I have a site where users can purchase points/credits to be used for a service on the site. Each customer has their balance displayed in their account in a transactions page, this page gets it's figures from a custom table from what I gather. What I'm essentially trying to do is have the 'remaining balance' figures displayed on each of my users pages, displayed on a single page.

I'm not sure if it's safe to paste the code here.. but if it is I will if it helps. Thanks for taking the time to read this request!

Topic table custom-taxonomy custom-field Wordpress

Category Web


Because it is a custom table, you write a custom function: Put this function in your themes functions.php. Then at the position in any template where you want to display the balance: displayRemainingBalance( $currentUserId )

function displayRemainingBalance($userid) {
  global $wpdb;
  $record = $wpdb->get_var($wpdb->prepare('select balance from yourcustomtable WHERE userid=%d', $userid));
echo $record->balance;
}

You can also use $wpdb->get_results( ... ) to get more than one row, or $wpdb->get_row( ... ) to get one row with multiple fields. The query "select balance from yourcustomtable WHERE userid=%d" depends on what table(s) you have and how you define the loggedin user.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.