WordPress Redirect to Mobile Site

In a very short time Google will start to emphasize sites that are specifically designed to be viewed on mobile. In many cases that will be by the inclusion of a mobile style sheet, so that the user will see the same content, but in a design that fits with their user experience.

But what do you do if you have a mobile specific site?

Google does have some directs on https://developers.google.com/webmasters/mobile-sites/mobile-seo/configurations/separate-urls?hl=en, but if you have a read through these, they seem to be more based around static sites, or sites with only a very few pages. So what happens if you have several hundred pages that need to be redirected to the same page but on a different url, eg https://www.example.com needs to be redirected to http://example.mobi AND https://www.example.com/page1.htm needs to redirect to http://example.mobi/page1.htm.

It’s actually easier than it looks, but weirdly there doesn’t seem to be any published code for it, so here goes!

First I should add that in my particular case the mobile site (http://example.mobi) was a static site, and already had the canonical tags added. Each page referred to it’s parent page as follows: –

<link rel="canonical" href="http://www.example.com/page1.htm" >

So on that side of things I didn’t need to do anything. Also the page url (by which I mean page1.htm, page2.htm etc), were also exact equivalents. If one site had something like page1/ or page1.html though, you could apply the same technique.

If you mobile site is also wordpress based, again the same code can be used as long as page names are the same.

Adding the link rel=”alternative”

Google recommends that: –

On the desktop page (http://www.example.com/page-1), add:

<link rel="alternate" media="only screen and (max-width: 640px)"
      href="http://m.example.com/page-1" >

One a larger site, or in wordpress that mean that you need either something to manually add in the mobile url, or you need to generate it automatically. I chose to do the latter as the site has over 250 pages! To do this you will need to edit your header.php file.

  1. Before you start, whatever you are doing take a backup of your header.php.
  2. I would recommend editing this file using an external editor like Dreamweaver or Coda and uploading it. That way if anything does go wrong, you can just upload the previous version. However, this is amazingly simple, so I didn’t follow my own advise and I actually edited it in Appearance >> Editor, then chose my theme and header.php
  3. Scroll down in the code to a place where you want to add your line in, add a few spaces so you have room and paste the following: –

$urlDesktop = array(“https://www.”, “com”);
$urlMobile = array(“http://”, “mobi”);
<link rel=”alternate” media=”only screen and (max-width: 640px)” href=”<?php echo str_replace($urlDesktop, $urlMobile, get_site_url() . $_SERVER[‘REQUEST_URI’]); ?>” >

In this case what we are doing is: –

  1. Creating 2 arrays. These are basically 2 places to store the elements in the url you want to swap out. So in my case urlDesktop contains the sections of the desktop url I wanted to swap, and urlMobile contains the sections I want to swap in. NOTE that the positions must be exactly the same ie the first item in one array swaps with the first in the other etc. You can make as may changes as you want in here as long as each array has the same number of elements. Also note that the Desktop array includes the “.” after “www”. If you leave that off, you will get “http://.example” in your final result. Not what you want.
  2. Add in the code as per Googles advise, but the link includes some php. This
    1. Requests the URL of the page that the code is on
    2. Then swaps out sections you built in your arrays, swapping the urlDesktop sections with the urlMobile sections.

That’s it! Just save your header.php file and you will now get the required Google code on each of your pages, pointing to the mobile site.

What to do if your urls don’t match

So if you have a site as large as the one I’m working on, there are a few pages where the urls don’t exactly align. Typically that might be when you get something like: –

  • Desktop site – http://www.example.com/page-1.htm
  • Mobile site – http://www.example.com/page1.htm

The easy way to handle this, if there are only a few of them is to rename the pages! However, for whatever reason you may decide not to do this. You could use your same arrays and just add the pages in there. eg

$urlDesktop = array(“https://www.”, “com”, “page-1”);
$urlMobile = array(“http://”, “mobi”, “page1”);

If you do this, make sure there are no page-10’s though as these would then be swapped to page10’s.

Adding a Default Category to a WordPress Custom Post

I have been searching for a way to add a default category into a WordPress Custom Post and finally got to a method that seemed to work for me. The explanation I came across was a bit vague so I thought I would try and clear it up here: –

The Code

// Adds the category to the jobs post automatically

function add_jobs_category_automatically($post_ID) {
global $wpdb;
$cat = array(139);
wp_set_object_terms($post_ID, $cat, ‘category’);
add_action(‘publish_job’, ‘add_jobs_category_automatically’);


line 1: function add_jobs_category_automatically($post_ID) {

This just sets the name of the function, in this case “add_jobs_category_automatically” declares it as a function and says what it applies to.

CHANGE ‘add_jobs_category_automatically’ to anything that will make sense to you.


line 2: global $wpdb;

Ok, I’m guessing at some of these, but I would say that this means that the function applies globally to the wordpress database

CHANGE: Nothing


line 3: if(!has_term(”,’category’,$post_ID)){

Some WordPress gubbins!

CHANGE: Nothing


line 4: $cat = array(139);

This is the category number you want to apply to the custom post. In my case it was 139, but yours could be anything.

To find the category number in your WordPress admin interface, go to Posts, Categories and mouse over the category you want to add automatically. You should see a number under category&tag_ID=139, the number is what you need (if you can’t see it by mousing over copy the link and paste it somewhere, it should look something like http://www.mysite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=139&post_type=post. Just copy the number and paste it into this line.

CHANGE: The number to your category


line 5: wp_set_object_terms($post_ID, $cat, ‘category’);

Some WordPress gubbins

CHANGE: Nothing


line 6, 7 just closing brackets
CHANGE: Nothing


line 8: add_action(‘publish_job’, ‘add_jobs_category_automatically’);

This is the tricks one, so I will break it into 3 parts

add_action – Some WordPress Gubbins – CHANGE: Nothing

publish_job – This says what your custom post type is called that this should apply to. In my case it is the job in publish_job. You will need to change the job to your custom post type.

To find this go to your custom posts, to the page where they are listed. The URL of the page is key. In my site it is http://www.mysite.com/wp-admin/edit.php?post_type=job. You need what comes after the post_type= in this url. As you can see mine is job.
CHANGE: publish_job, the job to your custom post type.

add_jobs_category_automatically – This is the function you created in line 1, so you should change this to the function name you made there. In my case line 1 was “function add_jobs_category_automatically($post_ID) {“, so you can see that the bit you need is between “function” and “($post_ID)”
CHANGE: All of it to your function name in line 1


You need to add your final code into the functions.php file, somewhere before the closing ?> tag. In mine I added it just after the function to create the custom post. So that whole section looks as follows: –

// Add Careers Custom Post Type

add_action( ‘init’, ‘register_cpt_job’ );
function register_cpt_job() {
$labels = array(
‘name’ => _x( ‘Careers’, ‘job’ ),
‘singular_name’ => _x( ‘Job’, ‘job’ ),
‘add_new’ => _x( ‘Add New’, ‘job’ ),
‘add_new_item’ => _x( ‘Add New Job’, ‘job’ ),
‘edit_item’ => _x( ‘Edit Job’, ‘job’ ),
‘new_item’ => _x( ‘New Job’, ‘job’ ),
‘view_item’ => _x( ‘View Job’, ‘job’ ),
‘search_items’ => _x( ‘Search Careers’, ‘job’ ),
‘not_found’ => _x( ‘No careers found’, ‘job’ ),
‘not_found_in_trash’ => _x( ‘No careers found in Trash’, ‘job’ ),
‘parent_item_colon’ => _x( ‘Parent Job:’, ‘job’ ),
‘menu_name’ => _x( ‘Careers’, ‘job’ ),
$args = array(
‘labels’ => $labels,
‘hierarchical’ => false,
‘description’ => ‘Careers section for displaying jobs on the site.’,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’, ‘custom-fields’, ‘revisions’, ‘page-attributes’ ),
‘taxonomies’ => array( ‘category’, ‘post_tag’ ),
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘show_in_nav_menus’ => true,
‘publicly_queryable’ => true,
‘exclude_from_search’ => false,
‘has_archive’ => true,
‘query_var’ => true,
‘can_export’ => true,
‘rewrite’ => true,
‘capability_type’ => ‘post’
register_post_type( ‘job’, $args );

// Adds the category to the jobs post automatically

function add_jobs_category_automatically($post_ID) {
global $wpdb;
$cat = array(139);
wp_set_object_terms($post_ID, $cat, ‘category’);
add_action(‘publish_job’, ‘add_jobs_category_automatically’);

The green text at the top is my custom post type and the blue test at the bottom is the code to set my default category in the custom post.

Is Google Sidelining Analytics?

With new tracking rules coming in from both Europe and I’m sure the rest of the world and modern browsers including a “do not track” feature it must be hard for Google to see a future for Analytics. Increasingly it seems that it is trying to depreciate the value of analytics by reducing the value of the information we collect, in favour of the information it collects.

Let me give you an example: –

  • Recent changes to Analytics have logged in users not passing their keyword information through to Analytics (although this isn’t evident in Adwords).
  • Google is encouraging people to stay logged in by increasingly pushing us to be logged in to use their services.

The end result of these two policies is of coarse that Analytics is seeing less and less keyword information, but Google is seeing more and more from it’s logged in users.

If I was to be cynical about this, I would say that it is an attempt by Google to still collect all the information it wants (as I’m guessing its in their terms of service), but not passing that information through as it has done before.

So what can we do about it? More and more it seems that Google is leaving itself open for new competition. In the analytics field there are paid options which are now starting to look quite attractive now we are hooked on the value of this type of information. If any of you out there have used any, please let me know. I’d be interested on your feedback.

Apple iSlate – What I’d Like to See

I always thought that the slate format would be great for business meetings or studying. Here’s what I would like to see for business: –

If they do it right you could flick documents to colleagues arranged around a virtual table, finally making a paperless office, and pushing Macs into the office space.

If you tie that in with a virtual keyboard, iChat for those that can’t be there and speech recognition to take notes for you, that would make life easy at work, or for studying.

On top of that an e-book reader would again help in studying, making the iSlate an ideal PC for those studying coarses that are more arts based and have less emphasis on computing horsepower.

What would you like to see?

Webmarshal Blocks Bing for Porn

I was amused to see this morning that our company webmarshal was blocking Bing for porn!

You may have heard all the fuss about this but you can look at pictures and video in Bing without going to the sites themselves, which makes Bing a proxy for porn.

I have to say though that blocking a search engine because you can find porn on it may be a bit harsh! Mind you it is Bing, so are you bothered?

iPhone OS3.0 – A Good Upgrade for iPod Touch Owners

A quick post to let you know that I have been playing with the iPhone OS3.0 on my 1st Gen iPod Touch for the last 24 hours.

I got the update for copy and paste really, but there is a lot of other stuff in there that makes it well worth the price.

Search is great, it is fast, suggests what you may be looking for and you can refine your search as you need. I can’t really work this out. How come on the Windows XP machine I use at work a search takes about an hour, but on the iPod it is instantaneous? Go figure!

iPod Touch owners also missed out in the 2.1 upgrade, as one of the main features, maps with street view, for some reason didn’t make the cut for the iPod. Well it’s in this time, and I have to say it works very well.

As for stability, so far everything is very stable with no crashes or any significant problems I can see.

A list apart article on user sign on

I have just written a comment on an article about user signup in a list apart, and thought I should share it with you.

Most of the posts were from users saying that they thought the user should be engaged first, so I wrote a post from the perspective of the company.

“I notice that you are giving opinions from the user perspective only and thought I could give a company perspective on this.

A web application I created a couple of years ago (www.ausrackid.com) went through this thought process. Ausrack ID allows IT pros to configure 19″ racking systems in a visual way, save print out the results, and get quotes from the company I work for.

I chose having no sign up until the user decides they want to save, at which point you are asked for username and email address, if they want a quote for items they get asked for more details. This is quite a way through the process. I was advised to put in a signup process at the front end to allow us to collect information on the users which we could potential use for emailing info. I resisted this at the time, and still do today. However, for the first year, the site was getting significant traffic, and significant usage, but very few people were saving their design, or asking for a quote. As the site needs to fund itself, it was very difficult to justify it’s existence at this stage, and the whole project was almost pulled.

I think the moral of the story is that your user details have a value, and giving those to a website you use may be the only way they can stay afloat. Think of youtube, they can justify their existence by the data provided by the number of users. That is why Google bought them. At the smaller scale my advice would be if they ask for it, and you want to use the service, give them your details, it might just help them survive in a competitive world.”

Is corporate greed going to drive Australia into recession?

I try not to write about the general economic circumstances, but I have to report back on this one. The company I work for deals with OneSteel an offshoot of BHP Billiton the mining and commodities giant. From March to June of this year we have seen prices rises of 44.9% on the steel we buy, with more expected to come in July.

The excuse from OneSteel is that it is there is a shortage in world supply. However reading the press, and their releases it seems like in Australia, they create the shortage by limiting shipping to their customers, this means that demand is always behind supply. Internationally it seems that they are trying to play the spot iron ore market in China to get better prices.

I’ve got to say that the chain goes like this: –

We buy steel – we increase our prices – our customers increase their prices – consumers pay more – inflation rises – risk of recession increases.

At the same time of coarse the bhp shareholders are raking it in from massively increased profits.

I’m not against capitalism, but you have to say that this is a really short term view from BHP. If they drive us into recession, they can forget house building, and many of the other industries they rely on for their bread and butter. I guess what I’m saying here is forget your short term profits and think of your market as a whole bhp. It’s up to you bhp, and many of the other corporates. Short terms gain and long term pain, or a more even market where you are more likely to still be here in 5 years time!