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

后端 未结 7 1047
悲哀的现实
悲哀的现实 2021-02-07 03:40

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

相关标签:
7条回答
  • 2021-02-07 04:01

    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;
    
    0 讨论(0)
  • 2021-02-07 04:11

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

    0 讨论(0)
  • 2021-02-07 04:14

    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.

    0 讨论(0)
  • 2021-02-07 04:15

    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_'
    
    0 讨论(0)
  • 2021-02-07 04:21

    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;
    }
    
    0 讨论(0)
  • 2021-02-07 04:24

    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();
    }
    
    0 讨论(0)
提交回复
热议问题