如何获得wordpress网站wp-config.php的文件路径


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

我正在开发一个插件,我需要在其中包括配置和查询点击wordpress数据库..我的问题是如何动态获取wp-config.php的url ..我用了

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

但是我得到错误

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

和我尝试了许多选项,如

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

我不知道确切的路径是什么。

总之,您不应该尝试从插件中包含wp-config.php。很少有好的理由让你采取这种方法。纯粹主义者甚至会说永远不应该这样做。

WordPress插件已经有适当的上下文来访问全局$wpdb变量进行查询。看一下这个例子:

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

建议阅读:

  • http://codex.wordpress.org/Class_Reference/wpdb

查询WordPress中的数据库应该通过全局变量$wpdb来完成。

如果您仍然希望避免所有警告以养成良好的编码习惯,您可以尝试包含许多路径的方法,其中可以找到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>');

指出:您可以使用if( !(include 'my_custom_path/wp-config.php') )

添加行

你不能使用wp_die()直到wp-config包含,然后使用php函数die()