admin管理员组

文章数量:1289541

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!

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!

Share Improve this question edited Jan 10, 2015 at 23:00 prettyfly asked Jan 10, 2015 at 22:21 prettyflyprettyfly 2261 gold badge2 silver badges7 bronze badges 2
  • 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 Commented Jan 10, 2015 at 22:39
  • Ahh, sorted it! Complete code for anybody else with this issue below! Cheers toscho! – prettyfly Commented Jan 10, 2015 at 23:08
Add a comment  | 

1 Answer 1

Reset to default 8

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

本文标签: customizationRetrieve and display data from custom db table in admin area