Stephanie Leary

Writer and WordPress consultant

  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Fascism Watch (2016-17)
    • Content Modeling for WordPress series
    • WordPress Hidden Gems series
  • Work
    • Portfolio
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →
  • About
    • Press Kit
  • Contact
    • Mailing List

Redirect imported content to WordPress URLs

May 25, 2016 Stephanie Leary Leave a Comment

Are you importing posts from another CMS? Do you want to avoid an .htaccess file with a million redirects? Of course you do!

Step 1: store your old pages’ paths in a custom field during your import.

Step 2: adapt this little function to your site. When a user lands on a 404 error, WordPress checks to see if the requested path exists in a custom field. If so, it redirects the user to the correct post.

In this case, my posts imported from Drupal had the old paths stored in the ‘drupal_path’ field.  Change your ‘meta_key’ to match your custom field name.

https://gist.github.com/sillybean/8f75af1cb1b0ba4b037616616127586e

Try visiting one of the old URLs. You should be whisked to the new location.

This function also prevents WordPress’s default behavior of trying to guess where to redirect an incorrect URL. If you want WordPress to keep doing that after it has checked for the custom field path, simply remove the “else return false;” lines.

Redirects for HTML Import

Since the HTML Import plugin stores the old URLs, you can use this to redirect your old files to the new WordPress pages:

https://gist.github.com/sillybean/06bd439a6a5b834bf02cec35208d0b39

Note the difference here: parse_url() isn’t used, because in this case the full URL has been stored, not just a relative path.

This works only if you have entered the old URL correctly in the plugin’s settings.

WordPress 404s, htaccess, html import, import, redirects

Leave a Reply Cancel reply

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

Fascism Watch

The Fascism Watch is a daily(ish) news roundup. View all the previous Fascism Watch posts »

Latest WordPress Book

Content Strategy for WordPress

A short book for content strategists and managers on implementing a complete content strategy in WordPress: evaluation, analysis, content modeling, editing and workflows, and long-term planning and maintenance.

Read the sample chapter

Kindle Nook iBooks Kobo Smashwords

WordPress for Web Developers

WordPress for Web Developers (9781430258667)

This is a book for professional web designers and developers who already know HTML and CSS, and want to learn to build sites with WordPress. The book begins with a detailed tour of the administration screens and settings, then digs into server-side topics like performance and security. The second half of the book is devoted to development: learning to build WordPress themes and plugins.

This is the second, much-revised and updated edition of Beginning WordPress 3, with a more accurate title. Everything’s been updated for WordPress 3.6.

WordPress for Web Developers is out now. See what's inside...

The best WordPress features you’ve never noticed

  • WordPress Hidden Gems: Screen Options
  • WordPress Hidden Gems: Bulk Edit
  • WordPress Hidden Gems: Private Status
  • WordPress Hidden Gems: Dashboard Feed Readers
  • WordPress Hidden Gems: Options.php

Content Modeling for WordPress series

  • Content modeling for WordPress, part 1: analyze content
  • Content modeling for WordPress, part 2: functional and organizational requirements
  • Content modeling for WordPress, part 3: a sample content model

This is an excerpt from Content Strategy for WordPress.My latest books are Content Strategy for WordPress (2015) and WordPress for Web Developers (2013). Sign up to be notified when I have a new book for you.

Copyright © 2021 Stephanie Leary