开发者

Wordpress plugin and database interaction

开发者 https://www.devze.com 2023-01-12 15:10 出处:网络
I am writing my first plug in and it was all going well unti I needed to pull some data from the WP database. Basically I have no idea how to do it, so I need a hand.

I am writing my first plug in and it was all going well unti I needed to pull some data from the WP database. Basically I have no idea how to do it, so I need a hand.

******EDIT****

// I need to connect to the DB to display records within the wp-admin panel > settings > myplugin's page

In my plugin I currently have:

$locations = $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_locations") );

but i get this error message: Call to a member function get_results() on a non-object in...

So I am guessing here that the $wpdb variable is not initiated, therefore I need to include the wp-blog-header.php (from what I have seen online this is what I need to do).

So I tried:

$absDIR = ABSPATH."wp-blog-header.php";
require_once($absDIR);
global $wpdb;

But then it starts crying about:

Call to a member function main() on a non-object in /path/to/functions.php on line 1504

So I am lost. Any help on how to interact with a database in a wordpress plugin would be bad as开发者_运维技巧s. Thanks


Fixed:

I was using the global $wpdb outside of the function that was actually using the it, which broke it.

I moved the global line to the top of the function and it worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号