1

I'm developing my first wordpress plugin where I can list all my employees with some CRUD functionality.

When I click "Add Employee" I get a new window where I can fill in all the fields for an employee and after that I click "Save"

But when I click save I get the following PHP error:

   Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\wp-custom-plugin\wp-content\plugins\werknemers\employee_crud_functions.php on line 15

And when I look at line 15 the code is:

 $wpdb->insert( ... )

and this is my form with the action set to that php page

 <form method="POST" action="<?php bloginfo('url') ?>/wp-content/plugins/werknemers/employee_crud_functions.php">

I assume that the file is not aware of the $wpdb variable. But how can I let that php page know what it is? Or am I using it all wrong?

3 Answers 3

2

You are quite possibly missing the global declaration of $wpdb.

From the wpdb reference.

WordPress provides a global variable, $wpdb, 
which is an instantiation of the class already 
set up to talk to the WordPress database. 
Always use the global $wpdb variable. 
(Remember to globalize $wpdb before using it in any custom functions.)

You need to use it like this.

global $wpdb;
//do something with it.
Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to mention that I declared that variable already...But in my case it's not a function, I just redirect to a page which reads my $_POST variables, do I need make a function for it and hook it in some kind of way?
It would be good if you just post the relevant code instead of us guessing. :)
I don't have the code with me now, but I was planning to do that when I'm at the computer where the code is on ;)
2

Do you have your $wbdb global? I know it's not very good practice to use global but most Wordpress plugins seem to work this way...

 function myFunction() {
       global $wpdb;
       $wpdb->insert(...);
    }

1 Comment

I forgot to mention that I declared that variable already...But in my case it's not a function, I just redirect to a page which reads my $_POST variables, do I need make a function for it and hook it in some kind of way?
0

you can include wp-config file like this include('../../../wp-config.php'); it works for me. here my folder's directory structure is F:\wamp64\www\Wordpress\custom_plugin_development\wp-content\plugins\CRUD-Plugin

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.