Retrieve and display data from custom db table in admin area?

So, i’m having a bit of an issue with a plugin i’m developing, I wonder if anyone can shed any light on the situation, as many, MANY searches have come back with zero help. :-/

Basically, my plugin creates several custom entities in the wordpress database, which all work fine, I have added a shortcode function to add a form to a page to submit information, which, again, works fine.

My problem is, when I wish to retrieve data from said custom entities, and display them in the admin section of WordPress (either in a dashboard widget, or a custom plugin page), it won’t work. Full stop.

My sample function to retrieve the data is as follows;

function showApplicants(){
global $wpdb;

$appTable = $wpdb->prefix . "applications";
    $query = $wpdb->prepare("SELECT * FROM $appTable WHERE %d >= '0'", RID);
    $applications = $wpdb->get_results($query);

    foreach ( $applications as $application ) 
{
echo $application->title . " " . $application->app_firstName . " " . $application->app_surName . "<br/>";
}

}

Strangely, when this code is put on a page outside of the WordPress admin area (for example, a WordPress created page, via a shortcode function (which simply outputs this code), or on a page I create and add in myself with this code in the template), it works! Retrieving the correct information, and displaying it as expected.

Add it to a dashboard widget, no joy. Add it to a custom page within the admin section, again, no joy.

I’m stumped. Any advice?

P.S – To avoid any confusion, the $wpdb global is declared further up in the code, so no need to declare it again (although, I did try declaring it again within the function, and it still didn’t work).

Any help would be very much appreciated!

Ta mucho!

1 Answer
1

Working code for adding widget to wp dashboard with information from custom DB:

/**
 * Add application widget to the dashboard.
 */
function addApplicationWidget() {
    wp_add_dashboard_widget(
                 'submitted_applications',         
                 'Submitted Applications',        
                 'showApplicants' 
        );  
}
add_action( 'wp_dashboard_setup', 'addApplicationWidget' );

function showApplicants() {
    global $wpdb;

    $appTable = $wpdb->prefix . "applications";
    $query = $wpdb->prepare("SELECT * FROM $appTable WHERE %d >= '0'", RID);
    $applications = $wpdb->get_results($query);

    foreach ( $applications as $application ) {
        echo $application->title . " " . $application->app_firstName . " " . $application->app_surName . "<br/>";
    }
}

Leave a Comment