简体   繁体   English

如何获得WordPress站点的wp-config.php文件路径?

[英]how to get a file path of wp-config.php of wordpress site?

I am developing a plugin in which i need to include config and query the wordpress database on click ..My question is How to get url of wp-config.php dynamically ..I have used 我正在开发一个插件,其中我需要包括配置并在单击时查询wordpress数据库。我的问题是如何动态获取wp-config.php的网址。

include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';

but i am getting error 但我遇到错误

include_once(): Failed opening /wp-config.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear')

and i have tried many options like 我尝试了很多选择

require_once('../../../wp-config.php');
require_once ("../wp-config.php");

I dunno wat is exact path .. 我不知道wat是确切的路径..

Any help is appreciated .. 任何帮助表示赞赏..

In short, you shouldn't be trying to include wp-config.php from your plugin. 简而言之,您不应该尝试从插件中包含wp-config.php。 There are few if any good reasons you would ever take this approach. 您几乎没有理由选择这种方法。 Purists would go as far as to say it should never be done. 纯粹主义者甚至会说永远不要这样做。

A WordPress plugin already has the proper context to access the global $wpdb variable for querying. WordPress插件已经具有适当的上下文来访问全局$wpdb变量以进行查询。 Take a look at this example: 看一下这个例子:

global $wpdb;

$results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}your_table WHERE column = %s", $your_value) );

Suggested reading: 建议阅读:

Querying the database in WordPress should be done through the global $wpdb variable. 在WordPress中查询数据库应该通过全局$ wpdb变量完成。 See the codex entry for that: http://codex.wordpress.org/Class_Reference/wpdb . 请参阅相应的法典条目: http : //codex.wordpress.org/Class_Reference/wpdb

If you still want to avoid all the warnings to make good practices of coding, you can try this that include many paths where can find wp-config.php 如果您仍然想避免所有警告以使编码更好,请尝试使用此方法,其中包括可以找到wp-config.php的许多路径。

if( !(include $_SERVER['DOCUMENT_ROOT'].'/wp-config.php') )
if( !(include 'wp-config.php') )
if( !(include '../../../wp-config.php') )
if( !(include '../../../../wp-config.php') )
if( !(include '../../../../../wp-config.php') )
    die('<H1>Can\'t include config.</H1>');

Notes: you can add lines with if( !(include 'my_custom_path/wp-config.php') ) 注意:您可以使用if( !(include 'my_custom_path/wp-config.php') )添加行

you can't use wp_die() until wp-config be included, then use php function die() 在包含wp-config之前,您不能使用wp_die() ,然后使用php函数die()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM