Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Integrating existing database into WordPress template pages WordPress

Hi there,

I am moving a PHP-driven site to WordPress using the Karma theme. The old (current) site has has these 2 pages:

[[LINK href=""]][[/LINK]]
[[LINK href=""]][[/LINK]]

They have special PHP code in the <head> and <body> tags to connect to an external database but I need to have them populate 2 WordPress pages instead.

I assume I need to create 2 new template pages (with a unique header.php file) but am not sure how to do that.

Any help is much appreciated. Thanks!

- Paul

Answers (4)


ej_emman answers:

hello pmycroft,

I can do this. I just pm you my details.


Duncan O'Neill answers:

You can dictate which header file is called in using the name ( slug ) of your pages. For example, if you have a page with a slug of about, it will look for, and use, header-about.php if that file is in the child-theme folder first, or the parent theme folder second.


Gabriel Reguly answers:

Hi Paul,

I have just done this to a client, it is fairly simple.

$other_db = new wpdb('user','password','database','server');
$query = 'select * from table where condition';
$row = $other__db->get_row( $query, ARRAY_A ); // for a single row result
$result = $other_db->get_results( $query, ARRAY_A ); // for a result set with more than one row
if ( sizeof( $result ) ) {
foreach ( $result as $row ) {
echo $row['column_one'] . ' ' . $row['column_two'];

No need to have a special header.php, for the title you can set it with this:

function title_from_query() {
return 'gabriel' . ' | ';
add_filter( 'wp_title', 'title_from_query' );

So you only need to create 2 template files, add your queries and HTML and you are set.


Gabriel Reguly comments:

Hi Paul,

To create your template files just upload 2 files to the server with a special comment inside them, and your code (PHP and HTML )

* Template Name: Stock

* Template Name: Stock Details

Let me know if you need more help.


pmycroft comments:

Hi Gabriel,

I understand how to create the 2 template files but don't understand why the header.php is the same. Doesn't it need to contain specific information regarding the database.

This is in the current page 1:

// set up the database connection [$db_name=, $connection=; $db= mysql_select_db($db_name, $connection);]
include( "/home/2/8/d/26771/26771/secure/sql.php" );


then the table with the results is in the <body>.

Maybe you can assist?

- Paul


Abdessamad Idrissi answers:

Hey Paul,

to solve your question the right way, we will break it into 2 simple parts:

1. Create this two template pages by inserting in your "functions.php" file this code:

// an array where you declare pages setting
// don't forget to create acutale files inside your theme; first.php and second.php
$custom_pages = array (
'post_title' => 'My first page title',
'post_name' => 'first_page_slug',
'_wp_page_template' => 'first.php'
'post_title' => 'My second page title',
'post_name' => 'second_page_slug',
'_wp_page_template' => 'second.php'

// launch page creation process
// note that you can call this function on certain cases
// here I call it only when we are in admin.
// (note that you must visit the admin once in order to create this templates)
if (is_admin()) :
test_create_pages( $custom_pages );

function test_create_pages( $custom_pages )
if( empty($custom_pages) ) return;

global $wpdb;

foreach ($custom_pages as $page )
// Prepare the page
$my_page = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'post_name' => $page["post_name"],
'post_title' => $page["post_title"]

// check if the page doesn't exist already
$sql = "SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '". $page["post_name"] ."' LIMIT 1";


if($wpdb->num_rows == 0) {

// Insert the page into the database
$page_id = wp_insert_post($my_page);

// Assign the page template to the new page
update_post_meta($page_id, '_wp_page_template', $page["_wp_page_template"]);

2. Open the two files and edit them to suite the needs of your script. To simplify things for you, go to your existing "header.php" page and copy it inside your created pages "first.php" and "second.php" then you can use the existing "single.php" to copy the rest of code to your pages and edit it to suite your needs.
Note that if you don't have "header.php" or "single.php" in your current theme, copy them from the default wordpress theme directory "twentyten".

Hope it helps.. bon chance :)