Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Non Object Error in Wordpress (https://www.warriorforum.com/programming/773165-non-object-error-wordpress.html)

Kirk Ward 31st March 2013 05:41 PM

Non Object Error in Wordpress
 
I am creating a small plugin to update a database table.

The updater scriipt works properly when included in a Wordpress page or post template, but not when included in a script run by a form action from the plugin. (action="script.php")

This is the error:
Quote:

fatal error call to a member function query on a non-object ...
where the error message references the fourth line of the code below where I am setting $result to the sql.
Code:

global $wpdb;
$table_name = $wpdb->prefix . "my_table_name";
$sql = "TRUNCATE " . $table_name;
$result = $wpdb->query($wpdb->prepare($sql));

From what I can find, which corresponds with what I suspected, the Wordpress framework, or whatever it is called, is not included in this small script, even though it is called by a plugin.

What is the correct way of getting the Wordpress functions, and framework, etc., loaded in this small script?

Many thanks for your consideration and help!
Kirk

Michael71 31st March 2013 06:29 PM

Re: Non Object Error in Wordpress
 
require( './wp-load.php' ); (edit the path)

Kirk Ward 31st March 2013 07:19 PM

Re: Non Object Error in Wordpress
 
Quote:

Originally Posted by Michael71 (Post 7919990)
require( './wp-load.php' ); (edit the path)

Awesome!

The script is at
Code:

/var/www/html/wp-content/plugins/my-plugin/my-file-name.php
and I used the server path,
Code:

/var/www/html/wp-load.php
It ran like a charm.

Next step is to get it to use a relative path. I'll keep working on that!

Many, many thanks!

Kirk Ward 31st March 2013 07:22 PM

Re: Non Object Error in Wordpress
 
P.S. - If the script allowed me to give more thanks, I'd have added three hundred and seventy-four just to start, with more to come later!


All times are GMT -6. The time now is 06:27 PM.