How to read values ​​from wp-config.php?

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

I currently have another file where I have this information, but I would like to use only wp-config .

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

+12
source share
7 answers

Here is the 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(); } 
+9
source

I even defined my own constants in wp-config.php and was able to get them in the subject without any inclusions.

WP-config.php

 define('DEFAULT_ACCESS', 'employee'); 

functions.php

 echo "DEFAULT_ACCESS :".DEFAULT_ACCESS; 

outputs DEFAULT_ACCESS: employee

+13
source

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; ?> 

It is assumed that you are on the same server and you can access wp-config.php through the file system.

If you are doing this for a plugin, these values ​​are already available. You will not need to include the file again.

+7
source

You can get all global constants from wp-config.php, just an echo const:

 <?php echo DB_HOST; echo DB_NAME; echo DB_USER; echo DB_PASSWORD; 
+3
source

If you want to connect to the database, for current versions of PHP it is recommended to use the mysqli extension (the mysql extension will depreciate):

 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
source

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

0
source

Here is the function to read all WP DB definitions:

 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
source

Source: https://habr.com/ru/post/900236/


All Articles