wordpress queries in mytheme/update.php

In my wordpress theme, I created a file called update.php. I can access this URL by
http://mydomain.com/wp-content/themes/mytheme/update.php

Here is the code inside update.php

Read More
global $wpdb;
// do some more stuff here
$wpdb->update( 'twitter_followers', array('count' => $followers), array('id' => '1') );
echo 'done';

When I go to this page I get this error.

PHP Fatal error: Call to a member function update() on a non-object in /var/www/vhosts/mydomain.com/subdomains/mytheme/httpdocs/wp-content/themes/mytheme/update.php on line 34 

Any ideas on how I can fix this?

Also note, I did created twitter_followers table in my database.

Related posts

Leave a Reply

1 comment

  1. The issue is that you don’t have $wpdb yet, since your update.php is not part of wordpress. When you go to update.php wordpress is never loaded, so you get the non-object error. Try adding this line to the top of your update.php file, obviously changing the path:

    require_once("/path/to/wordpress/wp-load.php");
    

    the path will probably look something like this in a normal installation:

    require_once("../../../../wp-load.php");