i am trying to display a custom widget on the dashboard, where the widget shows register date, user name and billing phone.
i found an old plugin that has the php code but doesn’t show the billing phone, so i add the billing phone as the following
<?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?>
the problem is am getting only the current login user phone for all other users.
How can i let it show the correct number of each user?
Here is the full plugin code that i edit to add to my theme functions.php
add_action('wp_dashboard_setup', 'od_dashboard_widgets'); function od_dashboard_widgets() { global $wp_meta_boxes; wp_add_dashboard_widget('od_user_widget', 'New User', 'od_dashboard_user'); } function od_dashboard_user() { global $wpdb; $usernames = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID DESC LIMIT 6"); ?> <table style="width: 100%"> <th><b>Registerd Date</b></th> <th><b>Name</b></th> <th><b>Phone</b></th> <?php foreach ($usernames as $username) { $userid = $username->ID ; ?> <tr> <td align="center"> <?php $reg_date = $username->user_registered ; echo date('M jS Y , h:i:s', strtotime($reg_date));?> </td> <td align="center"> <a href="<?php echo get_edit_user_link($userid); ?>"><?php echo $username->user_nicename ; ?></a> </td> <td align="center"> <?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?> </td> </tr> <?php } ?> </table> <?php }
this is a screenshot of the result am getting
Advertisement
Answer
Instead of using get_current_user_id()
, you should use $user->ID
So you get:
function action_wp_dashboard_setup() { wp_add_dashboard_widget( 'od_user_widget', // Widget slug. esc_html__( 'New user', 'woocommerce' ), // Title 'od_dashboard_user' // Display function ); } add_action( 'wp_dashboard_setup', 'action_wp_dashboard_setup' ); function od_dashboard_user() { // Args $args = array( 'orderby' => 'user_registered', 'order' => 'DESC', 'number' => 6 ); // Get users $users = get_users( $args ); // Output echo '<table style="width: 100%">'; echo '<tr>'; echo '<th>' . __( 'Registerd date', 'woocommerce' ) . '</th>'; echo '<th>' . __( 'Name', 'woocommerce' ) . '</th>'; echo '<th>' . __( 'Phone', 'woocommerce' ) . '</th>'; echo '</tr>'; // True if ( $users ) { foreach ( $users as $user ) { // Get user ID $user_id = $user->ID; // Output echo '<tr>'; echo '<td align="center">' . date( 'M jS Y, h:i:s', strtotime( $user->user_registered ) ) . '</td>'; echo '<td align="center"><a href="' . get_edit_user_link( $user_id ) . '">' . $user->user_nicename . '</a></td>'; echo '<td align="center">' . wc_make_phone_clickable( get_user_meta( $user_id, 'billing_phone', true ) ) . '</td>'; echo '</tr>'; } } echo '</table>'; }
Related: How to show WooCommerce new user registrations in WordPress Dashboard widget