Non Object Error in Wordpress

by 3 replies
4
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: 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
#programming #error #object #wordpress
  • require( './wp-load.php' ); (edit the path)
    • [ 1 ] Thanks
    • [1] reply
    • 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!
  • 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!

Next Topics on Trending Feed

  • 4

    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")