我正在开发我的第一个wordpress插件,我可以列出我所有的员工与一些CRUD功能。
当我点击"添加员工"时,我得到一个新窗口,在那里我可以填写员工的所有字段,然后我点击"保存"
但是当我点击保存时,我得到以下PHP错误:
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
看第15行代码是:
$wpdb->insert( ... )
这是我的表单动作设置为PHP页面
<form method="POST" action="<?php bloginfo('url') ?>/wp-content/plugins/werknemers/employee_crud_functions.php">
我假设文件不知道$wpdb变量。但如何让php页面知道它是什么呢?还是我用错了?
您很可能错过了$wpdb
的全局声明。
来自wpdb参考。
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.)
你需要这样使用它。
global $wpdb;
//do something with it.
是否有全局的$wbdb ?我知道使用全局不是很好的做法,但大多数Wordpress插件似乎都是这样工作的…
function myFunction() {
global $wpdb;
$wpdb->insert(...);
}
您可以像这样包含wp-config文件包括 ('../../../wp-config.php");这对我很有效。下面是我的文件夹的目录结构F: Wordpress ' wamp64 ' www ' ' custom_plugin_development ' wp-content ' plugins ' CRUD-Plugin