Convert from Blogger to WordPress

I recently converted this blog from Blogger (published via FTP) to WordPress. It actually turned out to be a whole lot easier than I anticipated. I’ll lay out the steps I took for you below.

Also, I should note, it was very important to me that all of my old Blogger links do a proper 301 permanent redirect to their new home on my WordPress blog. I cover this below. However, I do not cover how to convert your Blogger template to a WordPress template. I took the transition as an opportunity to create a new look for my blog. So I skipped that process.

Step 1

First! I installed WordPress on my NearlyFreeSpeech.NET hosting account. (Fact: NearlyFreeSpeech.NET is the best web host on the planet.)

Step 2

In Blogger (under Settings -> Publishing) I switched from “publish via FTP” to “publish on blogspot.com.”

Step 3

Then I used WordPress’s awesome Blogger Import feature! (Found under Manage -> Import) You’ll notice that you can not import blogger posts hosted via FTP. This is why I temporarily switched to blogspot.com.

Step 4

After the successful Import, I installed the “Maintain Blogger Permalinks” plugin. This plugin makes your new WordPress permalinks match your old Blogger permalinks. You only need to run this plugin once, then you can uninstall it.

Step 5

Now that my imported posts had their old permalinks, I took it a step further. I no longer wanted the archive folders (ie: /2006/03/) in my permalinks. Nor did I want my permalinks to end with the HTML file extension (ie: .html). So I added the following to the top of my .htaccess file.

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteRule ^([0-9]{4})/([0-9]{1,2})/([^/]+)\.html$ $3/ [QSA,R=301,L]
</IfModule>

Step 6

To preserve links to your old Blogger archives add the following to the top of your .htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^archives/([0-9]{4})_([0-9]{1,2})_([0-9]{1,2})_archive.html$ $1/$2/ [QSA,R=301,L]
</IfModule>

Optional Step 7

During your Blogger import into WordPress, all of your Blogger “labels” are converted into WordPress “categories.” So if you want to maintain any old links to your old labels, add the following to the top of your .htaccess file.

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteRule ^(labels)/([^/]+)\.html$ category/$2/ [QSA,R=301,L]
</IfModule>

I say this is optional because after I got my posts into WordPress, I immediately started changing all my categories and tags. So the precaution I took above turned out to be not very helpful. Oh well.

The End!

That about sums it up. It was a pretty easy transition. Of course I’ve spent hours setting up and tweaking my new template, and I’ve spent countless more hours playing with all the cool community created plugins. I just can’t help myself.

Drop me a comment if this was helpful to you! 🙂

How To: NHibernate Parent/Child Relationships

So you’re trying to setup a Parent/Child relationship using NHibernate. This is easy enough when you allow the foreign keys to be null in your database. Of course this is not desirable for data integrity so you disallow nulls. Now NHibernate is pissed because it wants to insert the child row first then update it with the parent id.

You can fix this by setting up a bi-directional relationship. Simply put, try the following.

In your Parent.hbm.xml add:

<set name="Children" inverse="true" cascade="all-delete-orphan">
 <key column="parent_id"/>
 <one-to-many class="Child"/>
</set>

In your Child.hbm.xml add:

<many-to-one name="Parent" column="parent_id" not-null="true"/>

Now you need to make sure you update the Child class to “know about” the Parent. To make this easy add this method to your Parent class:

public void AddChild(Child c)
{
 c.Parent = this;
 this.Add(c);
}

Now we can do this:

Parent p = new Parent();
Child c = new Child();
p.AddChild(c);
session.Flush();

That’s the tall and skinny of it. To really understand this concept check out Chapter 16 of the NHibernate Reference Documentation.

How to rename a table or column using T-SQL in Microsoft SQL

So yeah, you could use the Microsoft SQL Server Management Studio UI to rename your table or column. But sometimes you need to do the rename in T-SQL. Here’s how.

How to rename a table:

EXEC sp_rename 'OldTableName', 'NewTableName'

How to rename a column:

EXEC sp_rename
    @objname = 'TableName.OldColumnName',
    @newname = 'NewColumnName',
    @objtype = 'COLUMN'

For a more detailed explanation of sp_rename check out this MSDN article: http://msdn2.microsoft.com/en-us/library/ms188351.aspx.

If Then Else Shorthand in C#

Sometimes you need to use “If Then Else” logic on something really simple, but all those brackets and parentheses are ugly and confusing. So instead use some fancy shorthand!

In this example I want to display the top 5 items in my array, but if my array is less than 5 items long I want to show all of the items up to the length of my array. (Otherwise I’d get an “index out of bounds” error for trying to access array items that don’t exist.)

int showTopEntries = 5;

showTopEntries = showTopEntries >= totalEntries ? totalEntries : showTopEntries;

This reads as: If 5 is greater than or equal to my array’s length, then use my array’s length, else use 5.

Your significant other is gonna’ be really impressed with this bit of code! Well, unless your significant other is also a programmer… then my sarcasm is lost. :/

How to use Design view to edit a page inside nested Master Pages.

Here’s a quick tip. If you’ve used nested master pages in your ASP.Net web application and you’ve tried to enter the “Design view” in one of your nested aspx pages, you’ve probably seen this error message.

Design view does not support creating or editing nested master pages. To create or edit nested master pages, use Source view.

A simple trick to side step this issue is to clear the MasterPageFile attribute in your aspx file’s page declaration.

So this: MasterPageFile=”~/Example/Example.Master”
Becomes this: MasterPageFile=””

Now you can edit your page using the Design view. Just remember to put the MasterPageFile reference back when you’re ready to test or deploy your site.