Wordpress- can I include wp-config to get db details
I am making a change to an existing WordPress site on the checkout page so that when the user types a suburb/postcode, it looks up the list from a database.
Rather than writing the code to connect to MySQL, is their a PHP file I can include from the Wordpress directories that include the connection?
If not, is their any problem in including wp-config.php to get the database details and connect from their?
You can use WPDB after including the necessary files, change the directory to fit your needs:
include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php';
Oh and if you don't know what it is: http://codex.wordpress.org/Class_Reference/wpdb
I might add that a better way is to include those two lines to get access to all WordPress functions and classes:
define( 'WP_USE_THEMES', false ); require('wp-blog-header.php'); #adapt to match your path
The WP_USE_THEMES is set to false so that theme files aren't loaded.