Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 251
Steven Avatar asked Oct 27 '11 19:10

Steven


People also ask

How can I access config variable in php?

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.

What can you do with WP-config php?

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

How do I access WP-config php in cPanel?

Go to cPanel > File Manager. In the root folder of your WordPress installation, locate wp-config. php.


2 Answers

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

like image 192
ibex Avatar answered Oct 23 '22 21:10

ibex


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(); } 
like image 34
Virgil Shelton Avatar answered Oct 23 '22 21:10

Virgil Shelton