如何在Wordpress插件中包含$ wpdb? [英] How to include $wpdb in wordpress plugin?
问题描述
我已经在wordpress
中的plugin
上开发了一段时间,但是一个问题一直困扰着我.我想将数据库表导出为excel文件,因此我需要从插件目录中的文件访问全局$wpdb->variable
.
I've been developing for some time on a plugin
in wordpress
, but one problem keeps bugging me. I want to export a database-table as an excel file and therefor i need access to the global $wpdb->variable
from a file in my plugin directory.
我找到了一个博客条目,该条目解释了我应该包括哪些类,但这是行不通的(下面是链接).如您所见,我执行var_dump
,但从未达到该点.如果我在代码中未包含wp-config
和wp-load
的包含内容,则转储returns NULL
,所以我猜测导入存在问题.
I found a blog entry that explains what classes i should include, but this doesn't work (link is below). As you can see, i do a var_dump
, but it never reaches that point. If i leave the includes of wp-config
and wp-load
out of the code, the dump returns NULL
, so i'm guessing there is a problem with the imports.
无论如何,我希望有人可以帮助我解决这个问题.我的方法不一定需要修复,我只需要一种方法(可以从我的数据库中获取)导出数据数组以在WordPress中表现出色.任何帮助,将不胜感激.预先感谢.
Anyway, i was hoping someone could help me with this problem. I don't necessarily need a fix for my approach, I just need a way to export an array of data (fetched from my db) to excel in wordpress. Any help would be appreciated. Thanks in advance.
include_once('../../../wp-config.php');
include_once('../../../wp-load.php');
include_once('../../../wp-includes/wp-db.php');
var_dump($wpdb);
$filter = get_where_clause();
$order = get_order_by_clause();
$data = $wpdb->get_results("SELECT * FROM " . $table_prefix . "team_data" . $filter . $order, ARRAY_A);
$result = array();
我不能包含wp-config
,它会给出恒定的错误.我知道错误发生在哪里,我只需要找到一种解决方法.当查看wp-settings
页面(wp-config包含)时,您会发现以下代码行:
I cannot include the wp-config
, it gives constant errors. I know where the bug is taking place, I just need to find a work-around. when looking at the wp-settings
page (which is included by the wp-config) you will find this line of code:
foreach ( wp_get_active_and_valid_plugins() as $plugin )
include_once( $plugin );
unset( $plugin );
这是一个存在错误的地方.我只是不知道我应该如何解决这个错误.
this is where there is a bug. I just don't know how i should work around this bug.
问题解决了.当包含文件时,我多次包含了wp-config
(即使我声明只应包含一次).我通过使用以下代码解决了这个问题.
EDIT 2:
Problem solved. When including the file, i included the wp-config
more than once (even though i stated it should only be included one time). I solved the problem by using the following code.
global $wpdb, $table_prefix;
if(!isset($wpdb))
{
require_once('../../../../wp-config.php');
require_once('../../../../wp-includes/wp-db.php');
}
推荐答案
如果要创建WordPress插件,则无需手动添加这些文件.
If you're creating a WordPress plugin, you don't need to include those files manually.
如果要导出表,为什么不为它创建一个函数/类并将$wpdb
传递给它(如果需要).您还可以使用普通的 MySQLi 类(来自PHP)来访问您的MySQL数据库.
If you want to export your table, why don't you create a function/class for it and pass the $wpdb
to it (if you need it). You can also use the normal MySQLi-class (from PHP) do access your MySQL Database.
如果您只想使用WordPress使用的已存储登录值访问MySQL数据库,则可以在WordPress根目录中包含wp_config
文件.它具有一些(自解释)全局字段,可用于连接数据库:
If you simply want to access the MySQL Database with the stored login-values which are used by WordPress, you can include the wp_config
-file from the WordPress root-directory. It has some (self explaining) global fields which you could use to connect to your database:
include "WP-ROOT-PATH/wp-config.php";
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Test the connection:
if (mysqli_connect_errno()){
// Connection Error
exit("Couldn't connect to the database: ".mysqli_connect_error());
}
之后,您将拥有一个MySQLi类的实例(如上所述),可用于访问数据库.
After that you have a instance of the MySQLi-class (as mentioned above) which you can use to access your Database.
但是我不确定这是否是理想的选择,但是它确实有效.
I'm however not sure if this is the perfect way to go but it sure works.
要调试WordPress(如果某些操作不起作用并且没有错误消息),应在wp-config.php
文件中激活调试:
To Debug WordPress (if something doesn't work and there is no Error-Message) you should activate debugging in the wp-config.php
-file:
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define('WP_DEBUG', true);
此外,如果要在本地服务器上测试PHP脚本,则应在php.ini
文件中将display_error
转换为on
:
Also, if you're testing your PHP-Scripts on a local server, you should turn the display_error
to on
in your php.ini
-file:
; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments.
display_errors = On
但是,仅应在本地测试服务器上执行此操作,而不是在生产环境中进行此操作.
However this should only be done on your local test-server, not in a productive scenario.
这篇关于如何在Wordpress插件中包含$ wpdb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!