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?

Answers


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.


Need Your Help

iOS UIWebView - trap exceptions loading invalid file format

objective-c ios exception exception-handling uiwebview

I have a UIViewController containing a UIWebView which I am using to view various documents. Occasionally a document may attempt to be loaded which is not supported by the UIWebView (e.g. old Exce...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.