Inserting code blocks in to a WordPress post…..
by Billy on Sep.26, 2007, under Blogging
I have been working on writing the pages for some PHP and MySQL tutorials and was having a horrible time with the visual editor of WP destorying the code blocks and associated layout. But that is behind me now, and I will post a sample snippet of code here to show what it will look like on the future posts that contain code blocks.
To quickly explain what I have done though:
I installed an older version of coffee2code’s Preserve Code Formatting as well as installing the Quickcode plugin (from NetNerds.com). I followed their tutorial to get it setup and I have to say that I am indeed happy with test posts thus far. Hopefully you will find it good to use as well. I also should note that the entire procedure took me less than 10 minutes to download, upzip, install and configure.
Sample code:
Show/Hide: Meta Tags pulled from a MySQL database
function generateKeywords($page)
{
include_once 'mysql_dataconnector.php';
$connection = dataConnection('coxMetaInfo', 'headers') or die("Fatal Error on line 45 of site_functions.php --> Cannot create dataConnection");
$this->keywordQuery = "SELECT meta_keywords FROM headers WHERE headers.page_name = '".$page."'";
$keywordsResult = mysql_query($this->keywordQuery, $connection) or print ("Fatal Error on line 47: no keywords found");
$keywords = mysql_fetch_assoc($keywordsResult);
mysql_free_result($keywordsResult);
return $keywords;
}
function generateDescription($page)
{
include_once 'mysql_dataconnector.php';
$connection = dataConnection('coxMetaInfo', 'headers') or die("Fatal Error on line 56 of site_functions.php --> Cannot create dataConnection");
$this->descriptionQuery = "SELECT meta_description FROM headers WHERE headers.page_name = '".$page."'";
$descriptionResult = mysql_query($this->descriptionQuery, $connection) or print ("Fatal Error on line 47: no keywords found");
$description = mysql_fetch_assoc($descriptionResult);
mysql_free_result($descriptionResult);
return $description;
}
The nice thing is it will show a link that can show/hide the code block at will similar to what is on this post, and you can specify the title of that link.
I am going to note here that in testing you have to do several things to get the quickcode to work. If you are using the default Wordpress visual editor, you will need to stop and use the standard editor (changed under user profile). You cannot use the normal <quickcode></quickcode> tags as they will be stripped out. What I do is use the general editor and it works fine. Small price to pay for a coding oriented blog writer I guess. I am sure there may be a better method out there, but for now this was a good fix that solved the problem I was having.