21st June 2021
I wanted to redirect Google search results from WordPress.com to this blog. Ideally, one would add a "canonical link" in the head-tag of the HTML code. See for example stitcher.io: 07 — Be Searchable:
<head>
. . .
<link rel="canonical" href="https://stitcher.io/blogs-for-devs/07-be-searchable"/>
</head>
Unfortunately, WordPress.com does not allow this.
Next option was to add a link to each post in WordPress.com. WordPress.com now intentionally makes it difficult to conduct bulk-updates. You are forced to manually delete each post individually. For 300 posts in my case, I had to click two times each, i.e., 600 times:
To make the whole process even more awkward, WordPress.com now adds annoying pop-ups for each deletion. Obviously, this reinforces that leaving WordPress.com was the right decision and was overdue. Sad to see that WordPress.com is getting worse.
So the process to edit each posts via script was:
I used below simple Perl script to add a reference to this blog:
#!/bin/perl -W
# Insert link to eklausmeier.goip.de into each blog-post in WordPress export file
use strict;
my ($inItem,$year,$month,$day,$title) = (0,0,0,0,"");
while (<>) {
if (/^\s+<item>$/) { $inItem = 1; }
elsif ($inItem == 1 && /<link>https:\/\/eklausmeier.wordpress.com\/(\d{4})\/(\d\d)\/(\d\d)\/([\w\-]+)/) {
($year,$month,$day,$title) = ($1,$2,$3,$4);
#printf("\tlink: y=%s, m=%s, d=%s, t=%s\n",$year,$month,$day,$title);
$inItem = 2;
} elsif ($inItem == 2 && /^\s+<content:encoded><!\[CDATA\[(..)/) {
if ($1 ne "]]") {
my $url = "eklausmeier.goip.de/blog/$year/$month-$day-$title";
my $moved = "This post has moved to <a href=https://$url>$url</a>.\n\n";
s/^\s+<content:encoded><!\[CDATA\[/\t\t<content:encoded><!\[CDATA\[$moved/;
}
} elsif (/^\s+<\/item>$/) { $inItem = 0; }
print;
}
So in this script I search for the <item>
tag, then search for the <link>
tag, finally I search for <content:encoded>
. Then I simply insert the correct URL to this blog.
Categories: WordPress, Perl
Tags:
Author: Elmar Klausmeier