Import multiple WordPress posts with the same name

By default, the WordPress import plugin will not create multiple posts with the same name which were created on the same date. I’m not entirely certain why this is the default, non-configurable behavior but in an era where WordPress is so much more than a blogging engine there is no limit to the number of cases where it isn’t desirable. Maybe you have a post type for business locations that is arranged hierarchically and each location has a child page called “Contact Us.” Maybe a hundred other things.

In such cases, I’ve found moving a site from my production server to the live server is problematic, because the WordPress Importer plugin discards all but the first instance of a post. Fortunately, this behavior is easily changed with a small modification to the WordPress Importer.

When the plugin iterates through all of the items in the import file, it checks to see whether or not it should create a post for the current item. It only takes a minor tweak to make it always create the post.

In your plugins directory, find ./wordpress-importer/wordpress-importer.php and look for the following bit of code, currently on line 557 as of plugin version 0.6.1. In future versions the location might be different or it might be rewritten. I’ll try to keep this article up-to-date:

$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
...
}

Simply modify that as follows, and WordPress will create all posts as defined in the import file:

$post_exists = 0;
if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
...
}

By setting $post_exists = 0 you are telling the importer that the post doesn’t exist, which makes it work the way you probably want it to. I hope that this behavior will change (or be configurable) in future versions of the plugin, but for now this simple mod will get you going.

Redirecting from WordPress.com to a self-hosted blog

I am occasionally asked if Simple 301 Redirects will allow you to redirect your old blog on WordPress.com to your new self-hosted blog installed from WordPress.org. It won’t but there’s an easy way to do just that.

Simple 301 Redirects only works if you can install it on the site you’re redirecting from. Since you’re redirecting from a WordPress.com blog you won’t be able to install my plugin there. What you can do instead is utilize a premium upgrade offered by WordPress.com called Site Redirect, accessible from the WordPress.com dashboard.

As of January 2014 it costs $13 a year. What it does is redirect all of the URLs from your WordPress.com blog to the same URL at a domain of your choosing. It is, in essence, a domain-level redirect.

How long should you pay for it?

Determining how long to keep your Domain Redirect in place depends on where your traffic comes from. You should be OK to cancel it after a year or two if most of your traffic comes from search engines. Search engines will see the 301 redirect and update their indexes.

If most of your traffic comes from inbound links on other sites you may want to keep the redirect in place indefinitely so those links continue working forever.

Redirect all 404 pages to your WordPress home page

I’m occasionally asked to redirect every 404 error on a site to the home page. There are a couple of reasons why you might not want to do this (see below) but there are also valid cases where it could be the right move for your site. If you’ve decided it’s the right thing to do, here’s how to accomplish it easily in WordPress:

Edit or create 404.php in your theme directory. Replace all of the content inside with the following lines:

<php
wp_redirect(site_url(), 301);
exit();
?>

And that’s all there is to it!

Why shouldn’t you do that?

There are a couple of reasons that redirecting all 404 errors could be problematic.

First, search engines are said not to pass all of a page’s link equity to the redirected page unless the content is very similar. So redirecting pages that used to rank well to the home page won’t work as a sneaky way to boost your home page.

Probably more importantly, though, is the fact that it will be difficult to monitor which links are 404ing since they’ll all just end up on the home page. The usefulness of Google Webmaster Tools and other methods of monitoring 404 requests is not just in preventing future 404s. The real power is that it helps you identify what your users are looking for and where they’re failing to find it. Fixing all 404s in an automated way makes that more difficult.

Avoiding Timeout When Exporting Large WordPress Sites

When exporting large WordPress sites with thousands of posts and comments, I occasionally run into an issue where the WordPress export tool times out before it’s finished building the XML export file. This can usually be remedied by updating the config files for Apache and PHP. Temporarily increasing the amount of time allowed for a single request should solve the problem. Note that you’ll need access to your server’s config files to make these changes. If you don’t have access, explain to your web host what you’re trying to do and they should be able to help you out.

Assuming you have access, here are some values that you can try:

In the Apache config file, try setting Timeout 300, which means that a single request is allowed to take up to 300 seconds to process before it’s shut down.

You’ll have to make a similar change in the PHP config file. That line will look like max_execution_time = 300. While you’re in there, you may also want to increase the maximum amount of memory available to PHP for a single request. memory_limit = 256M should do the trick.

After you’ve made your config updates, restart Apache so that they take effect. Now you should be able to export your site without any problems. Be patient, as it can take several minutes to export a large site. The values above will give you up to 5 minutes. If it takes longer than that, the process will continue to timeout and you’ll have to make the values even larger.

It’s important to note that after you’re done with the export you should change the settings back to their initial values. No ordinary request should take 300 seconds. Leaving the timeout values that high will allow poorly written scripts to tie up Apache processes for a long time, which will seriously impact your server’s performance.

Batch Convert Domain Names to IP Addresses

Last week I needed to check a large number of sites to see how many of them were still hosted by my employer’s legacy system. After clicking through about 80 of them I realized it would have been much smarter to just ping them and see what IP address they were living at. I looked for a tool that would do that for me, but didn’t find one that would accept a list of domains and look them all up at once. So I made one.

DomainToIPConverter.com may be a new favorite among my tiny web apps. It accepts a list of host names and looks up the IP address of the server each is pointing to. That’s it. Please enjoy this potentially useful app that does exactly one thing.

Super Easy WordPress Breadcrumbs

Here’s a simple script to generate breadcrumbs for any page or custom post type that uses parent pages to establish hierarchy. I’ve opted to only show breadcrumbs on pages that have at least one parent. If your page doesn’t have a parent, nothing will show up.

To get started, paste this function into functions.php:

function easy_breadcrumbs($post) {
    $parent_id = $post->post_parent;
    if ($parent_id == 0) { return false; }
    else {
        $output = '';
        while ($parent_id != 0) {
            $ancestor = get_post($parent_id);
            $output = '<li><a href="'.get_permalink($ancestor->ID).'">'.$ancestor->post_title.'</a></li>' . $output;

            $parent_id = $ancestor->post_parent;
        }
        return '<ul class="breadcrumbs"><li><a href="'.home_url().'">Home</a></li>'.$output.'</ul>';
    }
}

To display the breadcrumbs, paste the following code into your template inside of the loop:

<?php echo easy_breadcrumbs($post); ?>

Now you have an unordered list. To make it look like you’d expect, you’ll need a little css. Something like this should do the trick:

.breadcrumbs {
	padding: 0;
	margin: 0;
}
.breadcrumbs li {
	display: inline-block;
	list-style: none;
}
.breadcrumbs li + li:before {
	content: ">";
	display: inline-block;
	margin: 0 .4em 0 .2em;
}

Get Just The Path from a Request in PHP

It can be a little tricky to get just the path of a request in PHP. $_SERVER['REQUEST_URI'] includes the query string. $_SERVER['SCRIPT_NAME'] may return index.php instead of the request if you’re using a CMS like WordPress which rewrites URLs.

The most reliable method I’ve found for returning only the path without the query string uses PHP’s built in parse_url() function:

$path_only = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

Uptime Robot WordPress Plugin

I’ve been using a simple, free uptime monitoring service called Uptime Robot lately to monitor a few of my sites. It will check up to 50 sites every 5 minutes to determine if they are online or offline, log any downtime events, and optionally notify you when your sites are down, all for free.

I wrote a quick WordPress plugin which creates a dashboard widget with your most recent Uptime Robot log entries. You can either monitor your entire account, or a single site. Just add the correct API key to the settings page.

You can download Uptime Robot for WordPress from the official plugin directory or through your WordPress Admin.

Get An RSS Feed Of Your Youtube Subscriptions

Updated July 7, 2015 – It looks like YouTube has disabled it’s combined subscription feeds. You can still download an OPML file of your subscriptions’ individual feeds and import all of them to your RSS reader. To find the OPML file visit YouTube’s Subscription Manager and click the “Export Subscriptions” button at the bottom of the page. The original post follows, but the instructions no longer work.

Following the announcement that Google Reader will be shutting down, I’ve been looking for a new place to read my content. I’m still not sure where I’ll land on that, but the change has me thinking about how I consume content as well. I’ve decided I’d like to do more of it in fewer places (the goal of RSS to begin with.) To that end, I’ve added my Youtube subscriptions to my feed reader. Here’s how you can get an RSS feed for your Youtube subscriptions, too:

Make Your Feed Available

First, make sure your feed is publicly available. Go to http://www.youtube.com/account_privacy and uncheck “Keep all my subscriptions private.”

Get Your User ID

Next, get your Youtube user ID at http://www.youtube.com/account_advanced. You’ll see your YouTube User ID listed.

Finally, replace {USER_ID} in the following address with your own User ID and you’ll have an RSS feed for new videos from your subscription list.

http://gdata.youtube.com/feeds/base/users/{USER_ID}/newsubscriptionvideos

 

Updated Fluid App Userscript for Gmail Unread Count

Screen Shot 2012-12-19 at 9.39.48 AMI’ve been enjoying using a Fluid App for my Google Mail account and I bought the Pro version for $5, primarily so I could get a badge in my dock with a count of unread messages. Unfortunately, the script that ships with the app to display this account no longer works, likely due to changes in Gmail itself. The original script parses the very complex markup to get the inbox value. My simplified script just reads the window title to determine the unread count. Here’s how to install the script:

  • Window > Userscripts
  • Create a new script
  • Pattern: “*mail.google.com*”
  • Script:
window.fluid.dockBadge = '';
setTimeout(updateDockBadge, 3000);
setInterval(updateDockBadge, 15000);

function updateDockBadge() {
	var title = document.title;
	var regex = /s*Inboxs*((d+))[^d]*/;
	var res = title.match(regex);
	if (res && res.length > 1) {
		var newBadge = res[1];
		window.fluid.dockBadge = newBadge;
	}
	else {
		regex = /^Inbox -/;
		if(regex.test(title)){
			window.fluid.dockBadge = '';
		}
	}
}

How it works:

It reads the window title and detects if you’re in the inbox and if there’s a number representing unread messages.

  • If so, it puts that number in the badge
  • If not, but you are in the inbox, it clears the badge
  • If you are viewing a message it does nothing to the badge since the unread count only shows on the inbox

This is obviously less technically clever than a script that reads the inbox count from the page markup directly, but it’s much less likely to break with changes to Google Mail, and much easier to understand and therefore fix if something does break.

As an aside, if you’re looking for a fancy Gmail icon, check out this set in the flickr Fluid Icon Pool.

Update: This works if you use English language settings for Gmail. Otherwise, you’ll have to change the word “inbox” to whatever is appropriate in your language.