php 如何在wordpress插件中包含$wpdb?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6217098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:40:32  来源:igfitidea点击:

How to include $wpdb in wordpress plugin?

phpwordpressplugins

提问by Michiel Standaert

I've been developing for some time on a pluginin 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->variablefrom a file in my plugin directory.

我已经在pluginin上开发了一段时间wordpress,但是一个问题一直困扰着我。我想将数据库表导出为 excel 文件,因此我需要$wpdb->variable从插件目录中的文件访问全局。

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-configand wp-loadout of the code, the dump returns NULL, so i'm guessing there is a problem with the imports.

我找到了一个博客条目,解释了我应该包含哪些类,但这不起作用(链接如下)。正如你所看到的,我做了一个var_dump,但它从未达到那个点。如果我离开代码的包含wp-configwp-load代码之外,转储returns NULL,所以我猜导入有问题。

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.

无论如何,我希望有人可以帮助我解决这个问题。我不一定需要修复我的方法,我只需要一种方法来导出一组数据(从我的数据库中获取)以在 wordpress 中表现出色。任何帮助,将不胜感激。提前致谢。

http://www.notesbit.com/index.php/web-mysql/web-scripts/standalone-access-the-wordpress-database-using-wpdb/

http://www.notesbit.com/index.php/web-mysql/web-scripts/standalone-access-the-wordpress-database-using-wpdb/

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();


EDIT: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-settingspage (which is included by the wp-config) you will find this line of code:

编辑:我不能包括wp-config,它给出了不断的错误。我知道错误发生在哪里,我只需要找到解决方法。查看wp-settings页面(包含在 wp-config 中)时,您会发现这行代码:

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.

这是存在错误的地方。我只是不知道我应该如何解决这个错误。

EDIT 2:Problem solved. When including the file, i included the wp-configmore than once (even though i stated it should only be included one time). I solved the problem by using the following code.

编辑2:问题解决。当包含文件时,我包含了wp-config不止一次(即使我说它应该只包含一次)。我使用以下代码解决了这个问题。

global $wpdb, $table_prefix;

if(!isset($wpdb))
{
    require_once('../../../../wp-config.php');
    require_once('../../../../wp-includes/wp-db.php');
}

回答by Lukas Knuth

If you're creating a WordPress plugin, you don't need to include those files manually.

如果您正在创建 WordPress 插件,则无需手动包含这些文件。

If you want to export your table, why don't you create a function/class for it and pass the $wpdbto it (if you need it). You can also use the normal MySQLi-class (from PHP) do access your MySQL Database.

如果你想导出你的表,为什么不为它创建一个函数/类并将它传递$wpdb给它(如果你需要它)。您还可以使用普通的MySQLi-class(来自 PHP)来访问您的 MySQL 数据库。



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:

如果您只想使用 WordPress 使用的存储登录值访问 MySQL 数据库,您可以包含wp_configWordPress 根目录中的-file。它有一些(自我解释)全局字段,可用于连接到数据库:

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());
}

After that you have a instance of the MySQLi-class (as mentioned above) which you can use to access your Database.

之后,您就有了一个 MySQLi 类的实例(如上所述),您可以使用它来访问您的数据库。

I'm however not sure if this is the perfect way to go but it sure works.

然而,我不确定这是否是完美的方式,但它确实有效。



To Debug WordPress (if something doesn't work and there is no Error-Message) you should activate debugging in the wp-config.php-file:

要调试 WordPress(如果某些东西不起作用并且没有错误消息),您应该在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);

Also, if you're testing your PHP-Scripts on a local server, you should turn the display_errorto onin your php.ini-file:

另外,如果你在本地服务器上测试你的 PHP 脚本,你应该在你的-file 中打开display_errorto :onphp.ini

; 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.

但是,这只能在您的本地测试服务器上完成,而不是在生产场景中完成。