Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For some reason the $wpdb object is not being defined in one particular function. I have four other functions in the file that are using $wpdb and all are working fine except this one simple delete function where I get the error: Call to a member function query() on a non-object... The following is the function in question:

function delete_gallery( $gallery_id ) {
    global $wpdb;

    $sql = "DELETE from galleries WHERE gallery_id = $gallery_id";
    $deleted = $wpdb->query( $sql );

} 

I have a link (delete gallery) on the plugin admin settings page that links to delete-gallery.php that calls the delete function and returns back to the admin page. The contents of that file are:

include 'ga-functions.php';

if (isset($_GET['gallery_id'])) {
    $gallery_id = $_GET['gallery_id'];
    delete_gallery($gallery_id);

    wp_redirect( $_SERVER['HTTP_REFERER'] );
    exit();
}

If I var_dump on the $wpdb after initializing I get the result of NULL. Anyone have any suggestions on what could be causing this in only one particular function like this.

share|improve this question
add comment

1 Answer

up vote -1 down vote accepted

Okay, new solution. First off, I'd recommend making the link a submit button and making the name something unique:

<submit name 'del_gallery' />

Then in the code of your admin page:

include 'ga-functions.php';
//checks to see if certain button was pressed
if(isset($_REQUEST['del_gallery'] && isset($_GET['gallery_id']) ) {     
    $gallery_id = $_GET['gallery_id'];
    delete_gallery($gallery_id);

    wp_redirect( $_SERVER['HTTP_REFERER'] );
    exit();        
}
share|improve this answer
    
Thanks for your replay Manny. After adding in the hook I'm getting the error: Call to undefined function add_action()... from the delete-gallery.php file –  gzhmr Oct 14 '12 at 5:43
1  
Okay, I see why. You are calling this php file without loading WordPress at all. That's why $wpdb and add_action are unavailable. I added an include to wp-load.php, which should sole the problem. –  Manny Fleurmond Oct 14 '12 at 12:12
    
Thanks again. I've tried that using the 'wp-load.php' but when I do it thows the errors/warnings: Use of undefined constant ABSPATH - assumed 'ABSPATH' in C:\xampp\htdocs\wordpress\wp-content\plugins\gallery-admin\includes\delete-galle‌​ry.php on line 3 .....AND..... Fatal error: require_once() [function.require]: Failed opening required 'ABSPATHwp-load.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\wordpress\wp-content\plugins\gallery-admin\includes\delete-galle‌​ry.php on line 3 –  gzhmr Oct 14 '12 at 12:54
    
okay, give me a sec and let me see what I can find out. By the way, is this php file in a plugin or function file? –  Manny Fleurmond Oct 14 '12 at 13:08
    
it's in a plugin file. –  gzhmr Oct 14 '12 at 13:14
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.