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
?
php include("config. php"); at the top of the PHP file you want to access it in. You will then be able to access the config variables form that PHP file it is declared in.
wp-config. php is one of the core WordPress files. It contains information about the database, including the name, host (typically localhost), username, and password. This information allows WordPress to communicate with the database to store and retrieve data (e.g. Posts, Users, Settings, etc).
Go to cPanel > File Manager. In the root folder of your WordPress installation, locate wp-config. php.
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
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With