wordpress 如何从 wp-config.php 读取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7921229/
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
How do I read values from wp-config.php?
提问by Steven
I need to get username, password etc from the wp-config
file to connect to a custom PDO database.
我需要从wp-config
文件中获取用户名、密码等以连接到自定义 PDO 数据库。
Currently I have another file where I have this info, but I would like to only use the wp-config
.
目前我有另一个文件,其中包含此信息,但我只想使用wp-config
.
So how can I read the different properties of wp-config
?
那么如何读取 的不同属性wp-config
呢?
采纳答案by Virgil Shelton
Here's some same code.
这是一些相同的代码。
// ...Call the database connection settings
require( path to /wp-config.php );
// ...Connect to WP database
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if ( !$dbc ) {
die( 'Not Connected: ' . mysql_error());
}
// Select the database
$db = mysql_select_db(DB_NAME);
if (!$db) {
echo "There is no database: " . $db;
}
// ...Formulate the query
$query = "
SELECT *
FROM `wp_posts`
WHERE `post_status` = 'publish'
AND `post_password` = ''
AND `post_type` = 'post'
";
// ...Perform the query
$result = mysql_query( $query );
// ...Check results of the query and terminate the script if invalid results
if ( !$result ) {
$message = '<p>Invalid query.</p>' . "\n";
$message .= '<p>Whole query: ' . $query ."</p> \n";
die ( $message );
}
// Init a variable for the number of rows of results
$num_rows = mysql_num_rows( $result );
// Print the number of posts
echo "$num_rows Posts";
// Free the resources associated with the result set
if ( $result ) {
mysql_free_result( $result );
mysql_close();
}
回答by ibex
I have even defined my own constants in in wp-config.php and managed to retrieve them in theme without any includes.
我什至在 wp-config.php 中定义了我自己的常量,并设法在没有任何包含的情况下在主题中检索它们。
wp-config.php
wp-config.php
define('DEFAULT_ACCESS', 'employee');
functions.php
函数.php
echo "DEFAULT_ACCESS :".DEFAULT_ACCESS;
outputs DEFAULT_ACCESS :employee
输出DEFAULT_ACCESS :employee
回答by Blake K Peterson
I would just include the file then I would have access to the variable in it varibales.
我只会包含该文件,然后我就可以访问其中的变量 varibales。
<?php
require_once('wp-config.php');
echo DB_NAME;
?>
This is assuming you're on the same server and you can access wp-config.php through the file system.
这是假设您在同一台服务器上,并且您可以通过文件系统访问 wp-config.php。
If you're doing this for a plugin, these values are already available. You won't need to include the file again.
如果您为插件执行此操作,则这些值已经可用。您不需要再次包含该文件。
回答by michelmany
You can get all the global constants from wp-config.php simply echo the const like that:
您可以从 wp-config.php 获取所有全局常量,只需像这样回显常量:
<?php
echo DB_HOST;
echo DB_NAME;
echo DB_USER;
echo DB_PASSWORD;
回答by AMIB
Hereis a function to read all WP DB defines:
这是一个读取所有 WP DB 定义的函数:
function get_wordpress_data() {
$content = @file_get_contents( '../wp-config.php' );
if( ! $content ) {
return false;
}
$params = [
'db_name' => "/define.+?'DB_NAME'.+?'(.*?)'.+/",
'db_user' => "/define.+?'DB_USER'.+?'(.*?)'.+/",
'db_password' => "/define.+?'DB_PASSWORD'.+?'(.*?)'.+/",
'db_host' => "/define.+?'DB_HOST'.+?'(.*?)'.+/",
'table_prefix' => "/\$table_prefix.+?'(.+?)'.+/",
];
$return = [];
foreach( $params as $key => $value ) {
$found = preg_match_all( $value, $content, $result );
if( $found ) {
$return[ $key ] = $result[ 1 ][ 0 ];
} else {
$return[ $key ] = false;
}
}
return $return;
}
this returns an array like this:
这将返回一个这样的数组:
array (size=5)
'db_name' => string '.........'
'db_user' => string '.........'
'db_password' => string '.........'
'db_host' => string 'localhost'
'table_prefix' => string 'wp_'
回答by VSB
If you want to connect to DB, for current versions of PHP, using mysqliextention is recommended (mysql extention is going to deprecate):
如果要连接到数据库,对于当前版本的 PHP,建议使用mysqli扩展(mysql 扩展将被弃用):
require_once ("../wp-config.php"); // path to wp-config depends on your file locatoin
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
回答by Nirav Patel
Just add required wp-load.php file. you can use all wordpress functionality like get_recent_posts() and many more...
只需添加所需的 wp-load.php 文件。您可以使用所有 wordpress 功能,例如 get_recent_posts() 等等...