I am trying to utilize this function. I cannot figure out why I am still getting the PHP error. All I can figure out is that for some reason the template isn't able to access $wpdb. I don't know what line needs to be placed in the functions.php (in theme folder) file that contains this function. Please help me!!

function submitWeightUpdate( $userid, $weight ) {
    global $wpdb;

    $result = $wpdb->insert( 'wp_weights', array( 'user_id' => $userid, 'current_weight' => $weight ), array( '%d', '%d' ) );

    if (empty($_POST['weight'])) {
        $return['error'] = true;
        $return['msg'] = 'You did not enter a weight.';
    else {
        $return['error'] = false;
        $return['msg'] = 'You\'ve entered: ' . $weight . ' as your new weight.';

    echo json_encode($return);

I also tried setting the theme to the default one and it still won't work, so I have no idea what could be wrong with it!

  • 1
    Please try this var_dump( isset( $wpdb ) ); in function and tell what it outputs.
    – Rarst
    Feb 13, 2011 at 20:25
  • Sorry I didn't reply earlier I didn't see your answer. I tried it without globalizing $wpdb, globalizing via " $GLOBALS['wpdb'] = $wpdb;", and globalizing via "global $wpdb". The result was always the same: bool(false). Could it be a permissions issue or something? In order to make the theme files editable inside of WP I had to set them chmod 767, wonder if I need to "reset" the permissions somehow or do some more modifications?
    – Brandon
    Feb 14, 2011 at 7:32
  • Hmmm... I am not quite sure why would $wpdb be uninitialized in this function, I assume rest of the site is working normally? How exactly you are running this code? In which template/whatever?
    – Rarst
    Feb 14, 2011 at 18:23
  • @Rarst: Those globals are unset mostly for two reasons: A) not yet initialized -or- B) because the object in there has been already destroyed (shutdown). And sure there is C) that someone just NULed/unset it which, well, can happen.
    – hakre
    Feb 14, 2011 at 18:34
  • @hakre yeah, I understand technical reasons why it would be unset, I don't get why would it be that way in one specific function if rest of stuff works fine. I suspect this is more of loading some file directly or AJAX stuff.
    – Rarst
    Feb 14, 2011 at 18:43

3 Answers 3


You did everything right with the globalize, the error message is just telling you that you called a function on $wpdb which does not exists.

Just check prior you do that $wpdb contains the object you're intersted in:

if (is_object($wpdb) && is_a($wpdb, 'wpdb')) {
    $result = $wpdb->insert( 'wp_weights', array( 'user_id' => $userid, 'current_weight' => $weight ), array( '%d', '%d' ) );

Alternatively you can add this below the global line to learn more:

global $wpdb;
var_dump($wpdb); // dump variable type and contents.

Additionally try:

require_once( ABSPATH . 'wp-load.php' );
global $wpdb;

You might not have wordpress ready to provide what you need in $wpdb.

  • In the conversation above the 2 guys figured out that I was using ajax and that was the root of the problem...now I just have to figure out how to properly either use ajax or rewrite my code to be able to pass text from my editbox...
    – Brandon
    Feb 14, 2011 at 23:49
  • Using AJAX is not the root of your problem. As using globals wasn't either.
    – hakre
    Feb 15, 2011 at 0:35
  • Ok, so your code will probably help. When I use require_once( ABSPATH . 'wp-load.php' ); I get an error about loading ABSPATHwp-load.php but when I change it to ../../../wp-load.php then I get an error saying /httpdocs/wp-content/themes/smartone/wp-admin doesn't exist. I assume wp-load is trying to load files based on the current directory which isn't working right. How do I make it load properly?
    – Brandon
    Feb 15, 2011 at 0:39
  • wp-load.php does only do what's coded inside of it. Just open the file with your editor, read the source-code in there and you will learn all you need to know.
    – hakre
    Feb 15, 2011 at 0:48
  • The exact error is "Failed opening required 'ABSPATHwp-load.php' (include_path='.:')"
    – Brandon
    Feb 15, 2011 at 0:54

It's likely that $wpdb has been globalized when it hits your particular function. Try checking $GLOBALS and if it there is an entry for $wpdb remove the line $global $wpdb; at the start of your funciton.

  • Thank you for your reply. I searched the themes functions.php file for "GLOBALS" and this is the only thing I came up with, I included the line above it to you could get an idea where it was located: function smartone_comment( $comment, $args, $depth ) { $GLOBALS['comment'] = $comment; I got the idea to try declaring it that way though when you said that so I changed "global $wpdb" to "$GLOBALS['wpdb'] = $wpdb;" and I now get "PHP Notice: Undefined variable: wpdb" as well as the original error.
    – Brandon
    Feb 13, 2011 at 17:14
  • So, if you do a print_r of GLOBALS at the start of your function -- what is the output? Feb 13, 2011 at 20:36
  • Ok I did it, I have uploaded the results to link. If I have the "$GLOBALS['wpdb'] = $wpdb;" line uncommented it adds "[wpdb] => " at the bottom of the list I uploaded.
    – Brandon
    Feb 13, 2011 at 21:10
  • @Brandon - Your output shows that global $wpdb is unset. Just in case you have not yet realized. There is no 'wpdb' entry in the print_r $GLOBALS output you posted.
    – hakre
    Feb 14, 2011 at 18:38
  • I know, if I add the "global $wpdb;" line it adds "[wpdb] => " at the bottom of the list I uploaded.
    – Brandon
    Feb 14, 2011 at 23:47

I faced this frustrating problem. None of the core functions were working.

So I included this:

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

As a better practice:

    $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
    $parse_uri = str_ireplace('index.php', 'wp-load.php', $parse_uri);
    require_once( $parse_uri[0]);
  • Please explain in what context you need this. This is (a) in 99% of all cases unnecessary and (b) error prone as all relative path mappings with .. dir changes are.
    – kaiser
    Jan 17, 2015 at 12:40
  • I needed this because wordpress core functions weren't loaded properly in the file I needed. None.
    – Kanika Sud
    Jan 20, 2015 at 9:07
  • There were some suggestions of including it with ABSPATH also. Guess that's a better practice?
    – Kanika Sud
    Jan 20, 2015 at 9:09
  • Yes, there are: Simply register your code as plugin, as mu-plugin or as theme. Without knowing the context I can't offer more than just this very general suggestion.
    – kaiser
    Jan 20, 2015 at 9:32
  • @kaiser: Finally used the code as stated in the edit above.
    – Kanika Sud
    Jan 20, 2015 at 14:55

Your Answer

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

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