i am new in wordpress. i want to run a sql from a php file and this file is calling from a plugin file. my plugin file code is:
if (confirm('Are You Sure You Want to Delete?')){
window.location.href = '../wp-content/plugins/delete_data.php?id=<?php echo $db_data['dynamicmenu_id']; ?>';
} else{
}
and this code is running from script.
my delete_data.php file code is given below:
dlt_opt();
function dlt_opt(){
global $wpdb;
var_dump($wpdb);
$dlt_id = $_GET['id'];
$result = $wpdb->query( $wpdb->prepare( "DELETE FROM ".$wpdb->prefix."dynamicmenu WHERE dynamicmenu_id=".$dlt_id ) );
}
but in delete_data.php is creating error because it is not finding $wpdb and is null. So wpdb is not initializing and it can’t find wpdb class. how can i add wpdb class?
Error message is:
Fatal error: Call to a member function query() on a non-object in ...htdocswordpresswp-contentpluginsdelete_data.php on line 7
Need help
Better to use require_once(ABSPATH . ‘/wp-load.php’);
I found my own answer. I need to include wp-load.php in that file.
although it’s not a good practice to delete like this.