How can I initialize wpdb class in a php file?

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:

Read More
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

Related posts

Leave a Reply

2 comments

  1. I found my own answer. I need to include wp-load.php in that file.

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

    although it’s not a good practice to delete like this.