0

I am running Custom Query on functions.php:

function subscribers(){
    if(isset($_POST["email"])){
        $email = $_POST["email"];
        echo $wpdb->insert($wpdb->easymail_subscribers, array('email' => $email));
    }
}
add_action('wp_ajax_subscribers', 'subscribers');
add_action('wp_ajax_nopriv_subscribers', 'subscribers');

This is being call by AJAX request. I got this error message:

Fatal error: Call to a member function insert() on a non-object in G:\xampp\htdocs\Projects\newsite\wp-content\themes\generic\functions.php on line 235

Tried really hard to solve this, but was not possible.

  • You have a "scope" problem. Look up "php variable scope". This answer may help. – s_ha_dum Nov 21 '13 at 4:53
1

You need to make sure you are using the global $wpdb

function subscribers(){
    global $wpdb;
    if(isset($_POST["email"])){
        $email = $_POST["email"];
        echo $wpdb->insert($wpdb->easymail_subscribers, array('email' => $email));
    }
}

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