How do I read values (PHP defined constants) from wp-config.php?

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.

Read More

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

Related posts

Leave a Reply

7 comments

  1. 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

  2. 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();
    }
    
  3. I would just include the file then I would have access to the variable in it 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.

    If you’re doing this for a plugin, these values are already available. You won’t need to include the file again.

  4. 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_'
    
  5. 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;
    }