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.

 <div>
&lt;div style=&quot;position: fixed; top: <span class='hiddenSpellError'>30px</span>; left: 30px; border: solid 2px #333; color: #000; background-color: yellow; padding: 5px; width: 400px; z-index: 5; font-family: verdana, geneva, arial, helvetica, sans-serif; font-size: large;"&gt;</div>
<pre>
<div><strong>My blog has moved!</strong></div>
</pre>
<div>You should be automatically redirected in 5 seconds. If not, visit
<a href='https://samueladesoga.wordpress.com/'> <span style="font-weight:bold;color:#bb0000;">https://samueladesoga.wordpress.com</span></a>
and update your bookmarks.</div>

</div>



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 = 'https://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 &gt; 0 &amp;&amp; m_postName.indexOf("_archive") &lt; 0) m_wpUrl += m_pubDate.getFullYear() + &#039;/&#039; + addZero(m_pubDate.getMonth() + 1) + &#039;/&#039; + addZero(m_pubDate.getDate()) + &#039;/&#039; + convertTitleToUrl(m_postName);

}
//redirect in 5 seconds
window.setTimeout(function(){location.href = m_wpUrl}, 5);

//change text
document.getElementsByClassName(&#039;redirectLink&#039;)[0].setAttribute(&#039;href&#039;, m_wpUrl);
document.getElementsByClassName(&#039;redirectText&#039;)[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:

gadget positions

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.

Advertisements
This entry was posted in Programming and tagged , , . Bookmark the permalink.

One Response to Redirecting blogger post to wordpress.com

  1. thank you: as of today, it still works perfectly!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s