How do I read values from wp-config.php?

2019-03-25 02:59发布

I need to get username, password etc from the wp-config file to connect to a custom PDO database.

Currently I have another file where I have this info, but I would like to only use the wp-config.

So how can I read the different properties of wp-config?

标签: wordpress
7条回答
Rolldiameter
2楼-- · 2019-03-25 03:51

Here is a function to read all WP DB defines:

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_'
查看更多
男人必须洒脱
3楼-- · 2019-03-25 03:54

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();
}
查看更多
Ridiculous、
4楼-- · 2019-03-25 03:54

If you want to connect to DB, for current versions of PHP, using mysqli extention is recommended (mysql extention is going to deprecate):

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;
}
查看更多
▲ chillily
5楼-- · 2019-03-25 03:58

Just add required wp-load.php file. you can use all wordpress functionality like get_recent_posts() and many more...

查看更多
叛逆
6楼-- · 2019-03-25 03:59

You can get all the global constants from wp-config.php simply echo the const like that:

<?php
  echo DB_HOST;
  echo DB_NAME;
  echo DB_USER;
  echo DB_PASSWORD;
查看更多
仙女界的扛把子
7楼-- · 2019-03-25 04:04

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

define('DEFAULT_ACCESS', 'employee');

functions.php

echo "DEFAULT_ACCESS :".DEFAULT_ACCESS;

outputs DEFAULT_ACCESS :employee

查看更多
登录 后发表回答