如何从 wp-config.php 读取值(PHP 定义的常量)?

2022-08-30 19:35:49

我需要从文件中获取用户名,密码等才能连接到自定义PDO数据库。wp-config

目前我有另一个文件,我有这个信息,但我只想使用.wp-config

那么我该如何读取?wp-config


答案 1

我甚至在wp-config中定义了我自己的常量.php并设法在没有任何包含的情况下在主题中检索它们。

wp-config.php

define('DEFAULT_ACCESS', 'employee');

函数.php

echo "DEFAULT_ACCESS :".DEFAULT_ACCESS;

产出DEFAULT_ACCESS:员工


答案 2

下面是一些相同的代码。

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

推荐