Redirecting blogger post to wordpress.com

I moved my blog from the blogger one to wordpress and i wanted to ensure that people that have bookmarked post on my old blog get redirected to the new one on wordpress as i wouldnt be update the old blog anymore.

I found a lot of effort by different people and all the stuff that was suggested using javascript didnt work for me, what i got was regardless of the page you are on my old blogger site, you would be redirected to my homepage on the wordpress site which is not very helpful.

With inspiration from this post, I decided to write my own piece of javascript to make sure that each post is redirected to its equivalent on the wordpress blog.



```
My blog has moved!
```
You should be automatically redirected in 5 seconds. If not, visit http://samueladesoga.wordpress.com and update your bookmarks.
function addZero(numberToPad) { return ("0" + numberToPad).slice (-2); } function convertTitleToUrl(title) { title = title.replace(/\'/g, "").replace(/\"/g, "") return title.replace(/^\s+|\s+$/g,"").replace(/\s+/g, "-").toLowerCase(); } var m_wpUrl = 'http://samueladesoga.wordpress.com/'; var m_published = document.getElementsByClassName('published'); var m_postName; if(m_published.length == 1) { m_published = m_published[0].getAttribute('title'); var m_pubDate = new Date(m_published); var m_url = location.href; var m_lastSlash = m_url.lastIndexOf('/'); var m_lastDot = m_url.lastIndexOf('.'); m_postName  = document.getElementsByClassName('post-title')[0].firstChild.data; if(m_lastDot > 0 && m_postName.indexOf("_archive") < 0) m_wpUrl += m_pubDate.getFullYear() + '/' + addZero(m_pubDate.getMonth() + 1) + '/' + addZero(m_pubDate.getDate()) + '/' + convertTitleToUrl(m_postName); } //redirect in 5 seconds window.setTimeout(function(){location.href = m_wpUrl}, 5); //change text document.getElementsByClassName('redirectLink')[0].setAttribute('href', m_wpUrl); document.getElementsByClassName('redirectText')[0].innerText = m_wpUrl;

In this post which i have taken inspiration from, the writer advises to put a gadget anywhere on your blog, that didnt work for me because sometimes where i try to do a javascript

document.getElementsByClassName(‘published’);

This part of the code fails because the page might not be fully loaded and if the gadget is placed above the post you want to redirect to its new location, the scripts would always fails.

So what I have done is to use two gadgets, use one gadget above the post and this gadget would contain the html bit that displays the information that the blog has moved and the second gadget I place lower down on the page; this is the gadget that  contains the JavaScript to be executed.

See image below:

Also in the post which i have taken inspiration from, the writer gets the url of the new wordpress post from the url of the blogger post, i have refused to do same as it seems that blogger tends to strip out certain words such as “the”. Hence my script would get the title of the blogger post which you are viewing and then attempt to create a slug with it would append to the new wordpress domain.

Hope you find this useful as well while it works.

You Might Also Like
Comments
comments powered by Disqus