Pergunta

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 ass. Thanks

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top