Dynamic WordPress content - getting content from another database

I am currently working on a research project to catalog a number of events from recent history. The goal was to be able to create these events in one database and display them. Due to the sophistication and layout capabilities of WordPress, this seemed like a great framework to put the presentation into. But there are a few hurdles I needed to overcome--essentially, how could I create pages in WordPress in advance, and then, when they were requested, source events them from another database.

The concept may or may not seem interesting, but my goal was to dig deeper into WordPress to understand some of the inner workings. Thanks to tons of great web sites where people share their knowledge (most notably John Faulds code example here), and WordPress's own outstanding documentation, it was easy to develop some simple code to perform the function.

First off, let me explain my approach. It was to create all the pages in WordPress first programmatically, placing an identifier into the content section. My goal would be to read this content section later, be able to determine what content to get from my secondary database based on this information, and then retrieve and display my new information.

All you need to do is go into your functions.php page and introduce code like the following (note John Faulds' runSQL function copied here almost verbatim):


function dynamic_content() {

$content = get_the_content();

echo "

";

$sql = "select description from dates where date = '" . $content . "'";
$results = runSQL($sql);

//echo $sql;

while ($row = mysql_fetch_array($results)) {
echo $row[0];
echo "

";
}

}


function runSQL($rsql) {
$rootpasswd='';
$user='';
$db='';
$dbcnx = @mysql_connect('localhost',$user,$rootpasswd,true);
if (!$dbcnx) {
echo '
Unable to connect to the database server at this time.
';
exit();
}
mysql_select_db($db, $dbcnx);
$result = mysql_query($rsql) or die ('test');
return $result;
mysql_close($connect);
}




This requires some explanation first. You will notice in your loop template pages that there are regular calls to "the_content". This function actually outputs content--and since my goal was to read this and replace it, the_content() is not acceptable.

1 comments:

WordPress Development said...

Interesting!
Your post is pretty useful for the CRE Loaded Development companies. Thanks for taking the time and effort.CRE Loaded Development

Post a Comment