Expanding Your WordPress Theme

I recently created a static webpage that looks exactly like my WordPress blog. So far, so good. There’s a slight problem. What if I decide to change my blog title or blog description? My static pages are going to be outdated. The following tutorial will show you how to make your static pages, well sort of dynamic.

The following code extracts your WordPress blog name, blog description and the URL from the WordPress database.

Code

<?php
// Database Configuration Settings
$host = "localhost"; // Default to localhost or servername
$db_username = "username"; // Your DB username
$db_password = "password"; // Your DB password
$database = "wp_options"; // WordPress Options Table
// Connect to WordPress Options Database
$db = mysql_connect($host, $db_username, $db_password);
if (!db) { die ('<p>Could not connect to MySQL database because of: '.mysql_error().'</p>'); }
mysql_select_db($database,$db);
// Get Blogname 
$result = mysql_query("SELECT option_value FROM $database WHERE option_name = 'blogname'",$db);
$row = mysql_fetch_array($result);
$blogname = $row["option_value"];
// Get Blog Description
$result = mysql_query("SELECT option_value FROM $database WHERE option_name = 'blogdescription'",$db);
$row = mysql_fetch_array($result);
$blogdescription = $row["option_value"];
// Get Home URL
$result = mysql_query("SELECT option_value FROM $database WHERE option_name = 'home'",$db);
$row = mysql_fetch_array($result);
$home = $row["option_value"];
mysql_close($db);
?>

Usage

To display the blog name, blog description or URL in your static page, just echo the variables shown below! Place this code in your static page. There you have it. A static page with a little bit of dynamism in it.

<?php
include('thisphpfile.php'); 
echo $blogname;
echo $blogdescription;
echo $home;
?>

1 thought on “Expanding Your WordPress Theme

Comments are closed.