techiva.blogspot.com
techiva.blogspot.com

Reputation: 1180

Fetch data from database show in wordpress Dashboard

I have created one menu in wordpress Dashboard using below code(taken from codex)

/** Step 2 (from text above). */
add_action( 'admin_menu', 'my_plugin_menu' );

/** Step 1. */
 function my_plugin_menu() {
add_menu_page( 'My Plugin Options', 'Personalization', 'manage_options', 'personalization-detail', 'my_plugin_options' );
}

Now I am able to get the current logged-in user information like this

function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) )  {
    wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
 global $current_user;

  echo 'Username: ' . $current_user->user_login . "\n";
  echo 'User email: ' . $current_user->user_email . "\n";

echo '</div>';

}

But I want to fetch data from some other table. Note I have placed all code within the functions.php

Upvotes: 0

Views: 2639

Answers (1)

Nikhil
Nikhil

Reputation: 1450

It depends on the table which you have.

  1. Retrieve data from WordPress default data structure.
  2. Retrieve data from your custom table.

Here is an example for retrieving data from a custom table.

global $wpdb;
$table_name = $wpdb->prefix . 'my_table';
$results = $wpdb->get_results( $wpdb->prepare('SELECT * FROM '.$table_name ) );
if ( $results ) {
    foreach ( $results as $result ){
        //handle data
    }

}

Upvotes: 1

Related Questions