1

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
    $wpdb->prepare() needs at least two arguments, and yes, you need a global $wpdb in your function, and that function must be called after that variable has been set by WordPress. – fuxia Jan 10 '15 at 22:39
  • Ahh, sorted it! Complete code for anybody else with this issue below! Cheers toscho! – prettyfly Jan 10 '15 at 23:08
7

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/>";
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.