Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Call to a member function get_results() on a non-object (jQuery From Plugin & WordPress)

I'm trying to use jQuery's Form plugin with in a Wordpress plugin. I'm following this example. I've enqueued my scripts and built my form. In csf_form_handler.php, the equivalent of the example's json-echo.php, I can access the items selected in my form (I have a radiobutton group).

My goal is to use the values selected in the form in a SELECT statement to return data from a custom wordpress database table.

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

global $wpdb;

$csf_db_table = $wpdb->prefix . "activity";


$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

Unfortunately, I'm getting:

Notice: Trying to get property of non-object (on the $wpdb->prefix line)

Fatal error: Call to a member function get_results() on a non-object (on the $csf_data line)

The code above in csf_form_handler.php isn't in a function. I don't know if that makes a difference.

How can I change the code so that I can use $wpdb?

Thank you.

like image 320
Laxmidi Avatar asked Nov 28 '22 22:11

Laxmidi


2 Answers

I came across the same issue but I got it resolved by including the wp-config.php file from the root of the WordPress folder like this:

require_once('../../../wp-config.php');
global $wpdb;

I hope this helps.

like image 51
Umar Niazi Avatar answered Dec 04 '22 07:12

Umar Niazi


When writing plugins, it's a good idea to keep your data-handling within the plugin's main file (i.e. not sending it to a separate file), and activate functions to handle it accordingly. Basically, you can set your form's action to point to either the plugin's file, or the page that contains the form.

Let’s assume this form you are working on is displayed on the front end of the site, on the sidebar. To handle data coming from that form when a user clicks “submit”, you could create a function in our plugin's file such as:

function $csf_get_data(){

global $wpdb; //since your this function is in your plugin’s file, $wpdb should be available, so no errors here! =)

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

$csf_db_table = $wpdb->prefix . "activity";

$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

    //do your stuff with $csf_data
}

//now run it everytime the plugin is run
if(isset($_POST[‘submit’])){
    $csf_get_data();
}

Now, you can set up your form action’s property to send the data to the same page, which will be able to handle it using the function above. You could use either:

    action=””

or

    action="<?php the_permalink()?>"

Please note: to make sure the data is coming from your site (especially public forms), remember to use wp_nonce_field() to create a nonce field that can be validated byt wordpress via wp_nonce(): http://codex.wordpress.org/Function_Reference/wp_nonce_field

Hope that helps,

Vq.

like image 35
Vidal Quevedo Avatar answered Dec 04 '22 09:12

Vidal Quevedo