Moving a single WordPress Category to a New Site

I recently moved the Fridley Farmer content from this site over to Xsite deletedX, and wanted to move all of my content over there too. There are plenty of instructions on moving wordpress sites available online, I did a full database dump, copied all the files, then deleted the non Fridley Farmer content from the new site. You can also use the Import/Export functionality to export just a single category, but I didn’t know if that would include my pictures and comments as well, so I just copied everything.

What wasn’t obvious however, was how to redirect users to my new content! I didn’t want real users visiting stuporglue.org for the Fridley Farmer content anymore, but I didn’t want to send EVERYONE (like you!) over to the new site. I also didn’t want Google thinking that my new site just had duplicate content.

I ended up making three changes to my wordpress install in order to make the transition as seamless as possible.

 

Header Redirects on Single Posts and The Cateogry

In theme’s header.php I added a short bit of PHP that would detect users trying to visit the Fridley Farmer category archive, or any post in the Fridley Farmer category. It performs a 301 Moved Permanently HTTP header redirect. Easy peasy.

<?php
if( is_single() && in_category('Fridley Farmer') ){
 header ('HTTP/1.1 301 Moved Permanently');
 $newsite = "http://newsite.com" . $_SERVER['REQUEST_URI'];
 header ('Location: '. $newsite);
}else if( is_category('Fridley Farmer') ) {
 header ('HTTP/1.1 301 Moved Permanently');
 $newsite = "http://newsite.com" . $_SERVER['REQUEST_URI'];
 header ('Location: '. $newsite);
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ...

Replace Category Content With a “We’ve Moved” Message

I edited the code in wp-includes/post-template.php to modify the post’s content as it’s returned.

I changed:

function the_content($more_link_text = null, $stripteaser = 0) {
   $content = get_the_content($more_link_text, $stripteaser);
   $content = apply_filters('the_content', $content);
   $content = str_replace(']]>', ']]&gt;', $content);
   echo $content;
}

Into:

function the_content($more_link_text = null, $stripteaser = 0) {
 if(in_category("Fridley Farmer")){
   $content = "<p>The Fridley Farmer has moved its wagon down the road to
   <a href='http://example.com'>http://example.com</a>. All of our
   belongings, posts and content have arrived safely, and we're just waiting for
   you!.</p>
   <p>Stuporglue.org will remain as a place for all my programming and other
   non-gardening related ramblings.</p>
   <p>So, update your links, tell all your friends and come check out the new site!</p>";
  } else {
   $content = get_the_content($more_link_text, $stripteaser);
   $content = apply_filters('the_content', $content);
   $content = str_replace(']]>', ']]&gt;', $content);
  }
  echo $content;
}

Replace Post Title Links With Link to New Site

In wp-includes/link-template.php I edited the very end of the function get_permalink. I made it so that links to Fridley Farmer content go to the new site.

return apply_filters('post_link', $permalink, $post, $leavename);
}

Into:

$finalurl = apply_filters('post_link', $permalink, $post, $leavename);

  require_once('category-template.php');
  if(in_category('Fridley Farmer',get_the_ID())){
    $finalurl = str_replace(home_url(),'http://fridleyfarmer.com',$finalurl);
  }

return $finalurl;
}

I hope that helps someone out. It wasn’t difficult, but I couldn’t find how to do something like this documented anywhere.

It will probably get overwritten the next time I upgrade WordPress, but hopefully by then everyone who needs to know to go to the new site will already have done so.

This entry was posted in Something Interesting and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *