Support » Developing with WordPress » PHP Fatal error: Uncaught Error: Call to a member function query() on null

  • Why $wpdb global returns null in this code? I can’t be able to do the mysql query because the global is null. Below the error in the error log file:

    PHP Fatal error: Uncaught Error: Call to a member function query() on null

    public static function get_classifieds( $urls ) {
            global $wpdb;
            $dom_document = new DOMDocument();
            foreach($urls as $url) {
                if( ! function_exists( 'wp_remote_request' ) ) {
                    $html = file_get_contents( $url );
                    $dom_document->loadHTML( $html );
                } else {
                    $response = wp_remote_request( $url );            
                    $body = wp_remote_retrieve_body( $response ); 
                    $dom_document->loadHTML( $body );
                $li_elements = $dom_document->getElementsByTagName( 'li' );
                foreach( $li_elements as $li_element ) {
                    $pattern = '/item\sresult/';
                    $class = $li_element->getAttribute( 'class' );
                    if( preg_match( $pattern, $class ) ) {
                        $img_elements = $li_element->getElementsByTagName( 'img' );
                        $h3_elements = $li_element->getElementsByTagName( 'h3' );
                        $h4_elements = $li_element->getElementsByTagName( 'h4' );
                        $p_elements = $li_element->getElementsByTagName( 'p' );
                        foreach( $h3_elements as $h3_element) {
                            $h3_class = $h3_element->getAttribute( 'class' );
                            $is_null = $wpdb->query( "SELECT IFNULL( SELECT * FROM '{$wpdb->prefix}classifieds' WHERE title = {$h3_element->nodeValue})" , true );      
                            if( $h3_class == 'title' && $is_null ) {
                                echo "<h3>$h3_element->nodeValue</h3>";
                        foreach( $img_elements as $img_element) {
                            $src = $img_element->getAttribute( 'src' );
                            if( strpos( $src, 'img' ) && ! strpos( $src, 'no-img' ) ) {
                                echo "<img src='$src' />";
                        foreach( $p_elements as $p_element) {
                            $p_class = $p_element->getAttribute( 'class' );
                            if( $p_class == 'description' ) {
                                echo "<p>$p_element->nodeValue</p>";
                            } elseif( $p_class == 'locale' ) {
                                echo "<p><strong>$p_element->nodeValue</strong></p>";
                            } elseif( $p_class == 'timestamp' ) {
                                echo "<p>$p_element->nodeValue</p>";
                        foreach( $h4_elements as $h4_element) {
                            $h4_class = $h4_element->getAttribute( 'class' );
                            if( $h4_class == 'price' ) {
                                echo "<h4>$h4_element->nodeValue</h4>";
Viewing 15 replies - 1 through 15 (of 20 total)
  • Where and when is this called? You have to be in the WordPress environment to have the WordPress global variables, and it has to be after the variables have been set to their values.

    This is a plugin that I’m developing and I called this global variable also in another file of this plugin and it work but in this case it returns null and it doesn’t work so why it doesn’t work?

    That didn’t answer the question of where and when it is called.
    Did you hook it to init? Did you hook it to wp_loaded?
    Is it a filter? Is it run on the admin page or on the front end?

    I’m sorry I answer instantly. It’s a function of a custom class into the boilerplate plugin of Devin Vinson. In the activator function the global variable works instead into the custom class it doesn’t work.

    Updating: I tried to require wp-db.php with ABSPATH constant but this results undefined. It’s the same result of global variable $wpdb as null: why this?

    You still didn’t answer the question, so it’s difficult to help. If you are working with someone else’s code, you should ask the author. Otherwise, we can only guess what you are doing.

    I try to explain better myself: I got Devin Vinson boilerplate plugin and I customized it with my code that you can see above. Now, my code returns this error because $wpdb global variable or ABSPATH constant isn’t visible or declared. But how can I include it in my custom code? Excuse me if I can’t be able to explain me.



    You are calling your class/function outside of WordPress. This typically happens when loading a custom PHP file using AJAX. This can be done, but it requires a lot of work if you want to use WordPress classes/functions/etc.

    If you want further help, you will need to show us the code that is calling your class/function.

    Ok @diondesigns and @joyously can you give me an email to add you to my github repository so I can share with you my code?

    No, you should just answer the questions “where and when is the code called?” Finding the answer to that question could help you to see what is wrong with the code.

    Ok I understand just so far that I use this global variable out of WordPress environment so it returns null. However now the question is: how can I use this global variable out of WordPress environment? Have I to include my function into an init hook? Or is there a ways to include $wpdb? I’m searching on the internet but probably my search is wrong.

    If you only need the $wpdb variable, you can either load it yourself or not use it. But you have other WP function calls in your code. So you need to figure out when you want this code to run. It could be on a cron schedule, or at plugin activation, or every time a post is saved. I don’t know what you want, but it seems to interface with WP so it likely needs to be on a hook that makes sense in the WP way of doing things.



    The $wpdb variable does not exist outside the WordPress environment. If you want to use it, you must load the WordPress code that creates the $wpdb variable. The actual code to do this is simple:

    define('SHORTINIT', true);
    require_once($path_to_wordpress . 'wp-load.php');

    The problem is defining the $path_to_wordpress variable. If you can be 100% certain that the location of your plugin is [wproot]/wp-content/plugins/[yourplugin], then it’s easy to create a relative path to wp-load.php. But WordPress allows sites to move the wp-content directory to locations outside the WordPress root, so you can’t be 100% certain unless your plugin will only be installed on sites you control.

    Note the definition of the SHORTINIT constant. That tells WordPress to load the database, cache, and hooks systems — and nothing else. Based on your code flow, you do not want to load all of WordPress…but if you really need it, then remove that line.

    Ok, I understand. How can you see I use more WP Functions, but none of this works because WP isn’t loaded. So I have to include or require wp-load.php, but how the other plugins do?



    Since I have no idea what you are doing in your plugin, I cannot answer your question. If this is due to an AJAX handler, it won’t occur in most plugins because they use admin-ajax.php for their AJAX handler.

    I also took a look at the boilerplate plugin you’re using. It turns a relatively simple task into a (needlessly) complex task, and it requires a fair amount of PHP knowledge. The phrase “trying to pound a square peg into a round hole” comes to mind. 🙂

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘PHP Fatal error: Uncaught Error: Call to a member function query() on null’ is closed to new replies.