Using SyntaxHighlighter to Format Code in WordPress

Based on a question in the StackOverflow beta site, I did some quick research into what are the best ways to perform syntax highlighting on code that is posted on blogs. Among the methods that were suggested (by myself or others):

  1. Hack together your own display logic to format it as you see fit
  2. Use the SyntaxHighlighter JavaScript library
  3. Use Windows Live Writer with the Insert Code plugin (I discuss that here)
  4. For WordPress, use the WP-Syntax plugin

Coincidentally, I had heard Scott Hanselman talking about how he does code formatting just a couple of days ago, in Hanselminutes #125, where he described how he posted code on his blog by putting it inside <pre></pre> tags, adding specific name and class attributes, and letting some JavaScript library do the formatting work. So I went to his blog, opened up a post with some code, and found my way to the SyntaxHighlighter JavaScript library. This is a very nifty library that handles formatting very nicely for a number of popular programming and scripting languages, and seemed to have a very easy implementation. So I decided to implement it for formatting code on my site.
Continue reading

Resetting a Lost MySQL Password

I sometimes use a Debian Ubuntu VMWare build to do some LAMP development. Due to my inexperience administering anything having to do with Linux, while trying to reset the root password, I accidentally put in some bad information into the password field (I forgot to use the password() function to generate encrypted password text), and ended up being locked out of the DB. After wading through a dozen different pages giving advice on how to reset a lost MySQL root password, I ended up finding a solution that worked for me:

  1. In terminal window with root access (in my case, using the sudo su command)
  2. # killall mysqld
  3. # mysqld –skip-grant-tables –user=root &
  4. Open new terminal window with root access
  5. # mysql (I am now logged into mysql as root)
  6. mysql> use mysql
  7. mysql> update user set password = password(“NEWPASSWORD”) where user=”root”;
  8. mysql> flush privileges;
  9. mysql> exit;
  10. # killall mysqld
  11. # /etc/init.d/mysql start

Definitely a relief to be able to successfully log back in again (and a good thing that this happened to me on my private test box and not on the public testing server).

Setting up a Customized Blog in 7 Easy Steps

When a person chooses to create a new blog, there are two routes to choose from: you can use one of the popular hosted blogging services (like Blogger or WordPress.com) or you host the blog yourself. The first option is definitely the easiest – someone else worries about all of the blog setup, database issues and hosting. You just put up the content. The second option however gives you more flexibility and control over the domain name, blogging software, plugins, advertising and template customization. However, when you set up the blog yourself, you become responsible for all aspects of its operation, a somewhat daunting proposition.

A couple of days ago I created a blog from scratch (Telecommuterer.com). I would like to review all of the different steps that went in to this. All told, it took about two hours to go from nothing to a fully functioning blog with initial content posted. Though it is more difficult than using a hosted blog service, I think that if you would like to use your blog for anything more than casual use and you are up to the task, in the end it is worth it to invest a little bit of time and money in setting up the blog by yourself.

I am assuming here that you already have the concept for the blog in place and a general idea of the types of content you would like to host. I will describe the process using blogging software from WordPress.org as I think it is the best free software available for the task (with easy setup, great plugin and community support).

  1. Get a Domain Name – Get a domain name for your blog. I use Go Daddy for all of my domain name purchases (though there are thousands of other providers that can do the same for you). For Search Engine Optimization (SEO) reasons, the shorter the domain name, and the closer it is connected to the main topic of your site, the better. If your first choices are taken, there are several services that will take your keywords and return a list of available names that might be good, like DomainsBot, DomainTools and DomainIt.
  2. Set Up Hosting – Set up your hosting for the website. The main requirements are that that you use LAMP (Linux, Apache, MySQL, PHP) based hosting. There are thousands of services out there that will do this for less than $15 per month. If you are using a service that is not well known, I highly recommend checking out their reputation on the web before using them. I personally use MediaTemples (gs) GridServer service and DreamHost for my personal and client sites and would have no reservations recommending them to others.
  3. Connect Your Domain – Set up a new Website at your webhost (they should have instructions on how to do this). Your host will give you their NameServer (or DNS) information. (This normally takes the form of something like ns1.hostname.com and/or an IP address). Take this information and go to the site where you bought your domain. On this site, in your domain management, change the Nameservers (or DNS settings) of the domain name to that which was given to you by your Web Host. After you do this, your domain name will now be connected to your website that is hosted on the servers of your Web Host.
  4. Install WordPress – Many hosting services have tools that will do this automatically via server-side scripting. If that is available and they offer it with the most recent version of WordPress (currently 2.2), go for it. Otherwise, you can do it yourself in a few easy steps
    1. Download the most recent version of WordPress
    2. Extract the files from the zip or tar.gz file onto your computer. Upload them to your web host servers using FTP
    3. Using phpMyAdmin or whatever other database control interface your web host provides, set up a new mySql database for your site.
    4. Follow the rest of the instructions on the WordPress 5 Minute Install page (it really does take just five minutes)

    After going to the /wp-admin/install.php page on your site and following the instructions there, you should have a working WordPress blog set up on your domain name with the default template in place (and absolutely no content). Log into your admin section (you may want to bookmark the page at /wp-admin/ on your site), go to the Options section (and its different subsections) to set up your blog settings as you would like (don’t forget to set Permalinks if you have the capability and want to use them to create nice-looking urls for your posts.

    Now the real fun begins.

  5. Set Up the Theme – If you are a real web-design pro and an expert with WordPress who can design their own theme from scratch, this doesn’t apply to you (and you don’t need to read any of this anyway). Otherwise, you should pick a theme for your site. There are hundreds of free WordPress themes out there to choose from. I recommend starting at the WordPress Themes viewer. It allows you access to test-drive hundreds of free themes and you can search based on features of the theme, layout options, etc. If it is too overwhelming, try filtering by theme features, using the list of most-downloaded themes (on the side of this page) or some of the themes featured here. When you have chosen a theme, download it to your computer and upload the files to your /wp-content/themes folder on your site. Go to the WordPress Admin > Presentation, and select your new theme.
  6. Customize the Theme – If you are scared of CSS and HTML and are completely satisfied with the way that the theme works, you can skip this step. Otherwise, it is advisable to go into your theme files and customize sections of it. This is done to differentiate the look and feel of your site from other sites that may be using the same theme, as well as to change the locations and look of elements of the theme to suit your specific needs.Any customization work that you will do will take place in the different theme files, found in /wp-content/themes/theme_name/. The files that you are most likely to change are header.php, footer.php, sidebar.php, index.php and single.php for content and styles.css for style changes. You can read about the different types of theme files, how they work, template tags and pages, customizing templates and more on the WordPress Codex and Support forums.The potential for changes is practically endless. To give some ideas of things to change here is a list of changes that I made on my new site
    • I changed made the font size larger for all content text
    • Removed “sponsor links” on the theme that were pointing to indecent sites (see Matt’s essay on this for more info)
    • Changed the RSS links to Feedburner feeds, added an Email subscription link (through Feedburner). Moved these links to higher on the page
    • Changed some of the default, hard-coded text on the page
    • Added some tags that had been left-out by the theme author (go to next page/post, etc)
    • Changed the format and information displayed in the post info section on both the main page and the single post page
    • Added links to stats tracking services (I use both StatCounter and Google Analytics)

    Some other things you might want to change in the theme to really change the look of it is the header image, background colors, link colors and other visual elements. As you are changing the template files, upload them to your site to check on how your changes look and make sure that you haven’t broken anything.

  7. Set up Plugins – As with the previous step, if you are completely satisfied with every aspect of your blog performance, you can skip this step (just remember to enable the Akismet anti-spam plugin that is included with the software). However, it is worth exploring the world of plugins if there is anything that you want to on your site that is not included by default or with the standard template tags. To set up any plugin, download its files, upload them to the /wp-content/plugins/ directory on your site, follow any specific directions that may be given with the plugin, go to the Plugins page in your site and enable the plugin. Some plugins work automatically in the background, some will require you to add tags to your template files (they should give instruction on how to do this).Plugins are hosted all over the internet, often on their author’s websites, and sometimes in centralized repositories. If you are in need of specific functionality, try searching for it (using keywords “[functionality] wordpress plugin” should give you some results if the plugin exists). Otherwise, try looking in some of the online repositories to get an idea of what is out there. Here is a list of the plugins that I used on my new site:

    Plugins can add lots of powerful functionality and totally change the way that you use your blog. They are one of the strongest features of WordPress and I highly recommend taking a look at what they have to offer.

You are now done setting up your blog (though as you use it more, you may decide to change are the template some more and add more plugins). Now all that’s left to do is to start adding content. Good luck!

Mint Popular Posts 0.93 Released

I have just released version 0.93 of the Mint Popuplar Posts plugin for WordPress. This version adds the ability to pull your Mint data from a DB other than the database in which you have installed WordPress (the most requested feature so far). (Cre

If your Mint install is located in the same DB as your wordpress data, there is no need to install this update.

For those trying out the plugin, please let me know if you run into any problems or bugs.

Anyone have requests for the next feature to add?

Deciphering PHP Error Messages and Blank Screens

My normal home is Visual Studio, ASP.net (1.1/2.0) and C#, with some CSS, HTML and JavaScript thrown in. Over the past week however, I have been working on an add-on to a client site written in PHP (using mySql as a DB backend).

I am coding in Dreamweaver and am really missing Visual Studio, intellisense, robust debugging, design-time compile, being told right away when I make syntax mistakes and intelligent error messages. Since I have to upload to the test server before trying anything out, I have had to spend too much of my time trying to figure out why errors are occuring, backtracking, commenting out lines, upload and try again.

To save myself some googling in the future, and perhaps to help others in the same quandry, below are some of the most common error messages (or lack thereof) that I experienced during development, along with what they actually mean (since the words themselves rarely tell you what you need to know). If anyone has more to add, put them in the comments and I will add them below.

Parse error: parse error, unexpected $ – You forgot to close all of your brackets. (Or maybe you wrote an open-bracket sign – { – instead of a close-bracket sign)

Completely Blank Screen – This one is the worst. It happens when you mess up php syntax. Note: the syntax error may be on the referenced page, or it may be on some page that you included, or that was included by an include (etc). The best part about it is you have no way of knowing where the error originated since the blank screen conveniently forgets to provide you with any error message. Here are the different things that caused this one to appear for me:

  • Forgot a semi-colon at the end of a line. (This one is the most frustrating, because it is sometimes very hard to find, and because if I was using VS, I would find out about it when I was compiling my site, and not when I was trying to test)
  • Include the word var in a function declaration. For example, the following caused me a few hours of searching to find the cause of my blank screen: “function (var $id) { …” (error is in bold).
  • Include a dollar sign when you aren’t suppossed to. For example: “$name = $account->$Name” causes an error.
  • Not including a dollar sign when you are suppossed to. For example: “for ($i=0; i<count($myArray); $i++) { …” causes an error (missing a dollar sign before the middle i)
  • Forgot a bracket (same as above, sometimes results in error message, sometimes in blank screen)
  • Entered <?php at the beginning of a document instead of <?PHP (stupid case-sensitivity…)

Parse error: parse error, unexpected T_ELSE – In writing an if/else structure, you forgot to include the closing bracket } between the end of if and the beginning of else

Parse error: parse error, unexpected ‘=’ – You forgot to include a $ in front of a variable when making a value assignment. For example: “i = 5;” instead of “$i = 5;”

WP: Mint Popular Posts

Mint Popular Posts is a WordPress plugin that for use in conjunction with the Mint. Mint is a php/mysql stats program that stores data (by default) in tables in the same DB that you would use for WordPress. Thus a WP-plugin to access the stats table and return a list of the most popular posts. Continue reading