1

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 ...\htdocs\wordpress\wp-content\plugins\delete_data.php on line 7

Need help

2

2 Answers 2

5

Better to use require_once(ABSPATH . '/wp-load.php');

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.