未来の自分のために備忘録として書いておく。
1.先人の記録
JUGEMからWordPressへURLそのままに移行したい
ロリポブログからWordPressにURLを変更せずに移行する
2.要は上記のブログの通りにやれや、という話なんですが、データベースだのSQLだのわからんわ、とか、過去の記事はともかくWordPressで書く記事のスラッグは自分で好きに設定したいとかいう場合には上記方法では困ります。
3.で、それを踏まえての注意点を。
4.functions.php(テーマのための関数)の末尾あたりにこいつを貼り付ける。
(※2025年11月19日更新。公開前のブログ投稿でプレビューが表示されないのを修正。
※2025年10月22日更新。月別アーカイブのリンクURLがおかしいことに今まで気付いてなかった)
/* JUGEMブログ風のURLにする */
class add_category_link_to_different_link {
function __construct(){
add_filter('query_vars', array(&$this, 'add_query_vars'));
add_action('parse_query', array(&$this, 'parse_query'));
}
public function add_query_vars($qvars) {
$qvars[] = 'cid';
return $qvars;
}
public function parse_query($query) {
$cat_slug = $query->query_vars['cid'];
if ( !(empty($cat_slug) || $cat_slug === 0) ) {
$query->query_vars["category_name"] = $cat_slug;
$query->is_404 = false;
}
return $query;
}
}
new add_category_link_to_different_link;
function my_term_link($termlink, $term, $taxonomy) {
return ($taxonomy === 'category' ? home_url('/?cid='. $term->slug) : $termlink);
}
add_filter('term_link', 'my_term_link', 10, 3);
class add_permalink_to_different_link {
function __construct(){
add_filter('query_vars', array(&$this, 'add_query_vars'));
add_action('parse_query', array(&$this, 'parse_query'));
}
public function add_query_vars($qvars) {
$qvars[] = 'eid';
return $qvars;
}
public function parse_query($query) {
// プレビューの場合は処理をスキップ
if (isset($query->query_vars['preview']) && $query->query_vars['preview']) {
return $query;
}
$post_slug = $query->query_vars['eid'];
if ( !(empty($post_slug) || $post_slug === 0) ) {
$query->query_vars["name"] = $post_slug;
$query->query["name"] = $post_slug;
$query->is_single = true;
$query->is_singular = true;
$query->is_home = false;
$query->is_404 = false;
}
return $query;
}
}
new add_permalink_to_different_link;
function my_post_link($permalink, $post, $leavename) {
// 公開済みの投稿のみカスタムURLを適用
if ($post->post_type === 'post' && $post->post_status === 'publish') {
return home_url('/?eid='.$post->post_name);
}
return $permalink;
}
add_filter('post_link', 'my_post_link', 10, 3);
// プレビューリンクのフィルターを追加(優先度を高く設定)
function my_preview_post_link($preview_link, $post) {
// 標準のプレビューリンク形式を強制的に使用
if ($post->post_type === 'post') {
return add_query_arg('preview', 'true', get_permalink($post->ID));
}
return $preview_link;
}
add_filter('preview_post_link', 'my_preview_post_link', 999, 2);
// get_permalinkをフィルタリングしてプレビュー時は標準URLを返す
function my_get_permalink($permalink, $post, $leavename) {
// プレビューコンテキストの場合
if (is_preview() || (isset($_GET['preview']) && $_GET['preview'])) {
// 標準のパーマリンク形式を返す
if (is_object($post) && $post->post_type === 'post') {
return home_url('/?p=' . $post->ID);
}
}
return $permalink;
}
add_filter('post_link', 'my_get_permalink', 1, 3);
add_filter('page_link', 'my_get_permalink', 1, 3);
add_filter('post_type_link', 'my_get_permalink', 1, 3);
// 月別アーカイブのリンクを変更
class add_month_archive_to_different_link {
function __construct(){
add_filter('query_vars', array(&$this, 'add_query_vars'));
add_action('parse_query', array(&$this, 'parse_query'));
}
public function add_query_vars($qvars) {
$qvars[] = 'month';
return $qvars;
}
public function parse_query($query) {
$month_param = $query->query_vars['month'];
if ( !(empty($month_param) || $month_param === 0) ) {
if (preg_match('/^(\d{4})\/(\d{2})$/', $month_param, $matches)) {
// ?eid=2025/09 形式で来た場合も処理
$query->query_vars['year'] = $matches[1];
$query->query_vars['monthnum'] = $matches[2];
$query->is_date = true;
$query->is_archive = true;
$query->is_home = false;
$query->is_404 = false;
} elseif (preg_match('/^(\d{4})(\d{2})$/', $month_param, $matches)) {
// ?month=202509 形式の場合
$query->query_vars['year'] = $matches[1];
$query->query_vars['monthnum'] = $matches[2];
$query->is_date = true;
$query->is_archive = true;
$query->is_home = false;
$query->is_404 = false;
}
}
return $query;
}
}
new add_month_archive_to_different_link;
function my_month_link($url) {
// /2025/09/ 形式を ?month=202509 形式に変換
if (preg_match('/\/(\d{4})\/(\d{2})\/?$/', $url, $matches)) {
return home_url('/?month=' . $matches[1] . $matches[2]);
}
// ?eid=2025/09 形式を ?month=202509 形式に変換
if (preg_match('/\?eid=(\d{4})\/(\d{2})$/', $url, $matches)) {
return home_url('/?month=' . $matches[1] . $matches[2]);
}
return $url;
}
add_filter('month_link', 'my_month_link', 999, 1);
// アーカイブウィジェットのリンクも変更(?eid=2010/01 形式も対応)
function my_archives_link($link_html) {
// ?eid=2010/01 形式を ?month=201001 形式に変換
$link_html = preg_replace_callback(
'/(href=["\'].*?\?eid=)(\d{4})\/(\d{2})(["\'])/i',
function($matches) {
return 'href="' . home_url('/?month=' . $matches[2] . $matches[3]) . '"';
},
$link_html
);
// /2010/01/ 形式を ?month=201001 形式に変換
$link_html = preg_replace_callback(
'/(href=["\'])([^"\']*?)\/(\d{4})\/(\d{2})\/?(["\'])/i',
function($matches) {
return $matches[1] . home_url('/?month=' . $matches[3] . $matches[4]) . $matches[5];
},
$link_html
);
return $link_html;
}
add_filter('get_archives_link', 'my_archives_link', 10, 1);
// ドロップダウン形式のアーカイブウィジェットのURLも変更
function my_archives_dropdown($archive_html) {
// ?eid=2025/09 形式を ?month=202509 形式に変換(完全URL対応)
$archive_html = preg_replace_callback(
'/(value=[\'"])([^\'"]*\?eid=)(\d{4})\/(\d{2})([\'"])/i',
function($matches) {
return $matches[1] . home_url('/?month=' . $matches[3] . $matches[4]) . $matches[5];
},
$archive_html
);
// /2025/09/ 形式を ?month=202509 形式に変換
$archive_html = preg_replace_callback(
'/(value=[\'"])([^\'"]*\/)(\d{4})\/(\d{2})\/?([\'"])/i',
function($matches) {
return $matches[1] . home_url('/?month=' . $matches[3] . $matches[4]) . $matches[5];
},
$archive_html
);
return $archive_html;
}
add_filter('wp_get_archives', 'my_archives_dropdown', 999, 1);
5.画像URLの書き換えだが、比較的新しい(2,013年12月頃~)記事だと
http://img-cdn.jg.jugem.jp/***/******/***.jpg
それ以前の記事だと
http://******img.jugem.jp/***.jpg
といったURLになってるのでがんばって一括置換してください。
6.JUGEMのエクスポートファイルの並び順と、管理画面の記事一覧の並び順が違う(昇順と降順で違うだけならいいけど、それ以外でも記事の更新日が効いてるみたい)ので、よくよく付き合わせる必要があります。気付かなかったら後で死ぬ。





