Non Object Error in Wordpress

3 replies
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:
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
#error #object #wordpress
  • Profile picture of the author Michael71
    require( './wp-load.php' ); (edit the path)
    Signature

    HTML/CSS/jQuery/ZURB Foundation/Twitter Bootstrap/Wordpress/Frontend Performance Optimizing
    ---
    Need HTML/CSS help? Skype: microcosmic - Test Your Responsive Design - InternetCookies.eu

    {{ DiscussionBoard.errors[7919990].message }}
    • Profile picture of the author Kirk Ward
      Originally Posted by Michael71 View Post

      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!
      Signature
      "We are not here to sell a parcel of boilers and vats, but the potentiality of growing rich beyond the dreams of avarice."

      Dr. Samuel Johnson (Presiding at the sale of Thrales brewery, London, 1781)
      {{ DiscussionBoard.errors[7920084].message }}
  • Profile picture of the author Kirk Ward
    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!
    Signature
    "We are not here to sell a parcel of boilers and vats, but the potentiality of growing rich beyond the dreams of avarice."

    Dr. Samuel Johnson (Presiding at the sale of Thrales brewery, London, 1781)
    {{ DiscussionBoard.errors[7920087].message }}

Trending Topics