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 Answer
Reset to default 8Working 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
版权声明:本文标题:customization - Retrieve and display data from custom db table in admin area? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741400234a2376621.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$wpdb->prepare()
needs at least two arguments, and yes, you need aglobal $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