Question

In main.js, I have a script, which is sending an ajax request to script.php.

$.ajax({
    type: "POST",
    url:  "wp-content/themes/roots/script.php",
    data: registerdata,
    success: function(result)
    {
        alert(result);
    }
});
return false;

script.php validate some data and write the data into the database. For this, I use the $wpdb object by WordPress ($wpdb->insert()). Normally, WordPress provides a global variable, $wpdb, which is an instantiation of the class already set up to talk to the WordPress database. But not in my case. I get the following error

Fatal error: Call to a member function insert() on a non-object

If I do

if(class_exists('wpdb') == false){
    echo 'class wpdb doesnt exist';
}
else{
    echo 'class wpdb exists';
}

in script.php, it told me all the time 'class wpdb doesnt exist'. If I do the same in function.php, class wpdb exists. But why? Script.php and function.php are in the same directory. And $wpdb is auto loaded and set up to global by WordPress. So I have access all the time, doesn't matter in which directory I am, right?!

realpath(ABSPATH . WPINC . '/wp-db.php')

.... also doesnt work. Constants are undefined in script.php, too. Where is my fault? register_globals = on in php.ini. changed nothing. :/

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top