1. Home »
  2. Tech

Guide to migrate Blogger mobile website to WordPress

WordPress

When migrating your blog from the Blogger platform to WordPress you may decide to maintain Blogger mobile links with the URL parameter (m=1). The main problem you will face is that the parameter (m) is a reserved WordPress parameter for the month archive. With this guide you will be able to override the parameter (m) and load a different mobile template with adding “?m=1” to your URL.

First you have to create a simple theme file inside your theme folder located in your root/wp-content/themes/theme-directory. This file can be created using notepad or any php editor like Adobe Dreamweaver and it must be with the extension “.php”. This is called a single page template where you create a single theme file to be loaded on certain conditions and you can name it “mobile.php”. This file will be responsible for loading your mobile template and the code inside it can be like the following example:


<html>
<head>
<title><?php wp_title( '|', true, 'right' ); ?></title>
</head>
<body>
<div id="content">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
          <h1 class="item"><span class="fn"><?php the_title(); ?></span></h1>
        <?php the_content(); ?>
    <?php endwhile; else: ?>
    <?php endif; ?>
</div>
</body>
</html>

After creating your mobile template successfully you must add the following code function to the file called “functions.php” inside your theme folder.


add_action(
	'pre_get_posts',
	'mobile_posts'
);
function mobile_posts($wp_query) {
	global $wpdb;
	if(!$wp_query->is_main_query()) {
		return;
	}
	$post_name = $wp_query->get('pagename');
	$post_type = $wpdb->get_var(
		$wpdb->prepare(
			'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1',
			$post_name
		)
	);
		$wp_query->set('m', '');
}
if (isset($_GET['m'])) {
add_filter( 'template_include', 'loadmobile_template');
function loadmobile_template( $template ) {
   global $post, $wp_query;
	if ($wp_query->query['m'] === '1') {
		$new_template = locate_template( array( 'mobile.php' ) );
		if ( '' != $new_template ) {
			return $new_template ;
		}
	}
	return $template;
}
}

This will use a WordPress function called “pre_get_posts” to override the parameter “m” and to load posts with this parameter while ignoring the month criteria. This code also use another WordPress function called “template_include” which will load the mobile single file template “mobile.php” with the URL parameter “?m=1”.

Now you will be able to load a mobile template similar to Blogger platform using the URL parameter “?m=1”. Example on the main post URL:


http://www.example.com/2014/01/post.html

Example on the mobile URL:


http://www.example.com/2014/01/post.html?m=1

To load posts with the parameter “m=1” automatically by detecting a mobile user you can add this code to the “functions.php” file.


if (wp_is_mobile()) {
if (!isset($_GET['m'])) {
wp_redirect('?m=1');
exit;
}
}