htaccess Redirects After Changing WordPress Permalink Structure

For my WordPress site, I had been using the “Day and name” permalink structure which includes the year, month, and day followed by the post name, e.g. /2013/07/06/sample-post/. When I re-installed WordPress due to a data loss, I changed to the “Post name” permalink structure, e.g. /sample-post/. Since the site had been previously indexed by search engines under the old permalink structure, the search results returned the old “Day and name” URLs which were now generating 404 response codes (Not Found).

In order to return the more appropriate response code of 301 (moved permanently) and redirect users to the new URL, you can modify the htaccess file to include redirects. This can be done by either creating a redirect for each specific page/post that moved or you can use a regular expression to execute the redirect from the older permalink structure to the new one.

As an example, I would add the following RedirectMatch line near the top of the htaccess file:

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.mysite.com/$4

If you’re using WordPress, then your htaccess file might have the following instructions:

Options All -Indexes

RewriteEngine On

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.mysite.com/$4

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Add Author Name to Twenty Eleven Theme “Posted On” Line

On WordPress sites using the Twenty Eleven theme where there is only one author, the “Posted on” line under the post title will hide the author name. If you want the author name to be displaed, then you can add the following to the child theme’s stylesheet (or change the Twenty Eleven theme’s stylesheet if you’re not using a child theme).

.single-author .entry-meta .by-author {
  display: inline;
}

WordPress Plugin to Send Notifications to Previous Commenters

This is a simple WordPress plugin to send an e-mail to all those who previously commented on a post. When an individual posts a new comment, everyone else who previously posted a comment will receive an e-mail containing an e-mail notification that a new comment was added to a particular post. It includes some minimal logic to send the notification to everyone except the individual posting the latest comment. While not included here, I would expect this can be further expanded to include features to opt-out of receiving notifications, to turn the notifications off for particular posts, or to modify the notification message content. Add the below code to a .php file and upload it to your plugin directory. Once uploaded, activate the plugin from your dashboard and the notifications will be sent when any new comments are posted.

<?php
/*
Plugin Name: WP Notify Previous Commenters
Plugin URI: https://www.dalesandro.net/
Description: WordPress plugin to notify previous commenters of new comments.
Author: DALESANDRO.NET
Version: 1.0
Author URI: https://www.dalesandro.net/
*/

add_action('comment_post', 'email_notification');

function email_notification($comment_id) {
  $emails = array();
  $comment = get_comment($comment_id, ARRAY_A);
  $comments = get_comments('post_id=' . $comment['comment_post_ID']);

  $original_post = get_post($comment['comment_post_ID'], ARRAY_A);
  $original_poster = get_userdata($original_post['post_author']);

  foreach($comments as $this_comment) {
    array_push( $emails, $this_comment->comment_author_email);
  }
  array_push($emails, $original_poster->user_email);

  $emails = array_unique($emails);
  $emails = array_diff($emails, array($comment['comment_author_email']));

  $headers = "From: " . $comment['comment_author'] . "\r\n";
  foreach($emails as $this_email) {
    wp_mail($this_email,
            html_entity_decode(get_bloginfo('name'), ENT_QUOTES) .
             " - New Comment Notification",
             "A new comment has been added to the post \"" .
             $original_post['post_title'] .
             "\" by " . $comment['comment_author'] . ".", $headers);
  }
}
?>