TopWordPress > カスタマイズ > その他 > WordPress 3.0にMovable Typeのタグをインポートする
2010年7月20日

WordPress 3.0にMovable Typeのタグをインポートする

Posted at July 20,2010 1:33 AM
Category:[その他]
Tag:[, , , ]

WordPress 3.0にMovable Typeの記事をインポートする際、Movable Typeのタグをインポートする方法を紹介します。WordPress 3.0でしか試していませんが、WordPress 2.xでも可能かもしれません。

1.概要

WordPress 3.0にMovable Typeの記事をインポートするには、「Movable Type and TypePad Importer」というプラグインを利用しますが、このプラグインでは次の機能しか提供されていません。

  • Movable TypeのキーワードフィールドをWordPressの記事のタグとしてインポート
  • Movable Typeのタグフィールドはインポート対象外

理由は、MTの途中のバージョンから、インポートフォーマットに「TAGS」というタグを示すフィールドが追加(タグ自体も途中のバージョンから追加)され、「Movable Type and TypePad Importer」プラグインがその仕様に追従していないためと思われます。

本エントリーのカスタマイズを行えば、次のようにMovable Typeのインポート形式のフォーマットに設定されたタグ(青色部分)を、WordPressにインポートすることができます。

Movable Typeのインポートフォーマット(サンプル)

AUTHOR: mtbook
TITLE: モバイルサイトオープン
BASENAME: post-9
STATUS: Publish
ALLOW COMMENTS: 0
CONVERT BREAKS: __default__
ALLOW PINGS: 1
PRIMARY CATEGORY: 子カテゴリ1
CATEGORY: イベント
CATEGORY: 子カテゴリ1
DATE: 09/13/2010 05:46:27 PM
TAGS: モバイル,キャンペーン
…後略…

インポート後の記事一覧画面

タグのインポート

このカスタマイズでは、キーワードフィールドの内容をタグにインポートしない制御もあわせて行います。

2.プラグインのインストール

管理画面より「ツール」→「インポート」をクリックして、「Movable Type と TypePad」をクリック。

インポート画面

ダイアログが開くので、右上の「いますぐインストール」をクリック。

タグのインポート

「Movable Type and TypePad Importer x.x のインストールが完了しました。」が表示されればインストール完了です。この状態では有効化されていないので、「プラグインを有効化してインポートツールを実行」をクリックするか(この段階ではインポートは実行しません)、プラグイン一覧画面でMovable Type and TypePad Importerを有効化します。

インストール完了画面

3.プラグインファイルの編集

wp-content/movabletype-importer配下にある、movabletype-importer.phpを編集します。まず、save_post関数の中にある2行を書き換えます。

変更前(赤色部分が変更対象)

function save_post(&$post, &$comments, &$pings) {
    $post = get_object_vars($post);
    $post = add_magic_quotes($post);
    $post = (object) $post;
 
    if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
        echo '<li>';
        printf(__('Post <em>%s</em> already exists.', 'movabletype-importer'), stripslashes($post->post_title));
    } else {
        echo '<li>';
        printf(__('Importing post <em>%s</em>...', 'movabletype-importer'), stripslashes($post->post_title));
 
        if ( '' != trim( $post->extended ) )
                $post->post_content .= "\n<!--more-->\n$post->extended";
 
        $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
        $post_id = wp_insert_post($post);
        if ( is_wp_error( $post_id ) )
            return $post_id;
 
        // Add categories.
        if ( 0 != count($post->categories) ) {
            wp_create_categories($post->categories, $post_id);
        }
 
         // Add tags or keywords
        if ( 1 < strlen($post->post_keywords) ) {
             // Keywords exist.
            printf('<br />'.__('Adding tags <em>%s</em>...', 'movabletype-importer'), stripslashes($post->post_keywords));
            wp_add_post_tags($post_id, $post->post_keywords);
        }
    }
    …中略…
}

変更後(青色部分が変更後のソースコード)

function save_post(&$post, &$comments, &$pings, &$tags) {
    $post = get_object_vars($post);
    $post = add_magic_quotes($post);
    $post = (object) $post;
 
    if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
        echo '<li>';
        printf(__('Post <em>%s</em> already exists.', 'movabletype-importer'), stripslashes($post->post_title));
    } else {
        echo '<li>';
        printf(__('Importing post <em>%s</em>...', 'movabletype-importer'), stripslashes($post->post_title));
 
        if ( '' != trim( $post->extended ) )
                $post->post_content .= "\n<!--more-->\n$post->extended";
 
        $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
        $post_id = wp_insert_post($post);
        if ( is_wp_error( $post_id ) )
            return $post_id;
 
        // Add categories.
        if ( 0 != count($post->categories) ) {
            wp_create_categories($post->categories, $post_id);
        }
 
         // Add tags or keywords
        if ( 1 < strlen($tags) ) {
             // Keywords exist.
            printf('<br />'.__('Adding tags <em>%s</em>...', 'movabletype-importer'), stripslashes($tags));
            wp_add_post_tags($post_id, $tags);
        }
    }
    …中略…
}

もうひとつ、process_postという関数を修正します。

変更前(赤色部分が変更対象)

function process_posts() {
    global $wpdb;
 
    $handle = $this->fopen($this->file, 'r');
    if ( $handle == null )
        return false;
 
    $context = '';
    $post = new StdClass();
    $comment = new StdClass();
    $comments = array();
    $ping = new StdClass();
    $pings = array();
 
    echo "<div class='wrap'><ol>";
 
    while ( $line = $this->fgets($handle) ) {
        $line = trim($line);
 
        if ( '-----' == $line ) {
            // Finishing a multi-line field
            if ( 'comment' == $context ) {
                $comments[] = $comment;
                $comment = new StdClass();
            } else if ( 'ping' == $context ) {
                $pings[] = $ping;
                $ping = new StdClass();
            }
            $context = '';
        } else if ( '--------' == $line ) {
            // Finishing a post.
            $context = '';
            $result = $this->save_post($post, $comments, $pings);
            if ( is_wp_error( $result ) )
                return $result;
            $post = new StdClass;
            $comment = new StdClass();
            $ping = new StdClass();
            $comments = array();
            $pings = array();
        } else if ( 'BODY:' == $line ) {
            $context = 'body';
        } else if ( 'EXTENDED BODY:' == $line ) {
            $context = 'extended';
        } else if ( 'EXCERPT:' == $line ) {
            $context = 'excerpt';
        } else if ( 'KEYWORDS:' == $line ) {
            $context = 'keywords';
        } else if ( 'COMMENT:' == $line ) {
            $context = 'comment';
        } else if ( 'PING:' == $line ) {
            $context = 'ping';
        } else if ( 0 === strpos($line, "AUTHOR:") ) {
            $author = trim( substr($line, strlen("AUTHOR:")) );
            if ( '' == $context )
                $post->post_author = $author;
            else if ( 'comment' == $context )
                 $comment->comment_author = $author;
        } else if ( 0 === strpos($line, "TITLE:") ) {
            $title = trim( substr($line, strlen("TITLE:")) );
            if ( '' == $context )
                $post->post_title = $title;
            else if ( 'ping' == $context )
                $ping->title = $title;
        } else if ( 0 === strpos($line, "STATUS:") ) {
            $status = trim( strtolower( substr($line, strlen("STATUS:")) ) );
            if ( empty($status) )
                $status = 'publish';
            $post->post_status = $status;
        } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
            $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
            if ( $allow == 1 )
                $post->comment_status = 'open';
            else
                $post->comment_status = 'closed';
        } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
            $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
            if ( $allow == 1 )
                $post->ping_status = 'open';
            else
                $post->ping_status = 'closed';
        } else if ( 0 === strpos($line, "CATEGORY:") ) {
            $category = trim( substr($line, strlen("CATEGORY:")) );
            if ( '' != $category )
                $post->categories[] = $category;
        } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
            $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
            if ( '' != $category )
                $post->categories[] = $category;
        …中略…
        }
    }
    …中略…
}

変更後(青色部分が変更後または追加となるソースコード)

function process_posts() {
    global $wpdb;
 
    $handle = $this->fopen($this->file, 'r');
    if ( $handle == null )
        return false;
 
    $context = '';
    $post = new StdClass();
    $comment = new StdClass();
    $comments = array();
    $ping = new StdClass();
    $pings = array();
    $tags = '';
 
    echo "<div class='wrap'><ol>";
 
    while ( $line = $this->fgets($handle) ) {
        $line = trim($line);
 
        if ( '-----' == $line ) {
            // Finishing a multi-line field
            if ( 'comment' == $context ) {
                $comments[] = $comment;
                $comment = new StdClass();
            } else if ( 'ping' == $context ) {
                $pings[] = $ping;
                $ping = new StdClass();
            }
            $context = '';
        } else if ( '--------' == $line ) {
            // Finishing a post.
            $context = '';
            $result = $this->save_post($post, $comments, $pings, $tags);
            if ( is_wp_error( $result ) )
                return $result;
            $post = new StdClass;
            $comment = new StdClass();
            $ping = new StdClass();
            $comments = array();
            $pings = array();
            $tags = '';
        } else if ( 'BODY:' == $line ) {
            $context = 'body';
        } else if ( 'EXTENDED BODY:' == $line ) {
            $context = 'extended';
        } else if ( 'EXCERPT:' == $line ) {
            $context = 'excerpt';
        } else if ( 'KEYWORDS:' == $line ) {
            $context = 'keywords';
        } else if ( 'COMMENT:' == $line ) {
            $context = 'comment';
        } else if ( 'PING:' == $line ) {
            $context = 'ping';
        } else if ( 0 === strpos($line, "AUTHOR:") ) {
            $author = trim( substr($line, strlen("AUTHOR:")) );
            if ( '' == $context )
                $post->post_author = $author;
            else if ( 'comment' == $context )
                 $comment->comment_author = $author;
        } else if ( 0 === strpos($line, "TITLE:") ) {
            $title = trim( substr($line, strlen("TITLE:")) );
            if ( '' == $context )
                $post->post_title = $title;
            else if ( 'ping' == $context )
                $ping->title = $title;
        } else if ( 0 === strpos($line, "STATUS:") ) {
            $status = trim( strtolower( substr($line, strlen("STATUS:")) ) );
            if ( empty($status) )
                $status = 'publish';
            $post->post_status = $status;
        } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
            $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
            if ( $allow == 1 )
                $post->comment_status = 'open';
            else
                $post->comment_status = 'closed';
        } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
            $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
            if ( $allow == 1 )
                $post->ping_status = 'open';
            else
                $post->ping_status = 'closed';
        } else if ( 0 === strpos($line, "CATEGORY:") ) {
            $category = trim( substr($line, strlen("CATEGORY:")) );
            if ( '' != $category )
                $post->categories[] = $category;
        } else if ( 0 === strpos($line, "TAGS:") ) {
            $tags = trim( substr($line, strlen("TAGS:")) );
        } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
            $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
            if ( '' != $category )
                $post->categories[] = $category;
        …中略…
        }
    }
    …中略…
}

4.インポートの実行

管理画面より「ツール」→「インポート」をクリックして、「Movable Type と TypePad」をクリック。

インポート画面

インポートするファイルを選択して「ファイルをアップロードしてインポート」をクリック。

ファイル選択画面

ユーザーを選択して「Submit」をクリック。

ユーザー選択画面

記事がインポートされ、タグがある場合は「Adding tags」が表示されます。

インポート実行画面

記事の一覧でタグがインポートされていることが分かります。

記事一覧画面

記事編集画面でもインポートされていることが分かります。

記事編集画面

2010.09.24
save_post修正後のソースコードに修正もれがあったので、追記しました。

Posted by yujiro   このページの先頭に戻る
関連記事
この記事を読んだ人はこんな記事も読んでいます
人気エントリー
Hatena Hot Entries
Hatena Entries
トラックバックURL


コメント

はじめまして。

こちらの情報を参考にさせていただき、プラグインを修正し、
無事にタグ情報をインポートすることができました。
大変助かりました、ありがとうございます。

プラグインの修正について、もう1箇所修正が必要な箇所がありました。
おそらく記載漏れだと思うのですが。。。

以下の部分です。

save_post メソッド
280行目付近

if ( 1 < strlen($post->post_keywords) ) {

if ( 1 < strlen($tags) ) {

それでは!

[1] Posted by husky : September 17, 2010 9:08 AM

>huskyさん
こんにちは。
ご返事遅くなってすいません。

コメント&修正のご連絡ありがとうございました。
最初に頂いたコメントのソースコードを修正して、後の2件のコメントを削除致しました。
それではよろしくお願い致します。

[2] Posted by yujiro logo : September 24, 2010 4:40 PM
コメントする
greeting

*必須

*必須(非表示)


ご質問のコメントの回答については、内容あるいは多忙の場合、1週間以上かかる場合があります。また、すべてのご質問にはお答えできない可能性があります。予めご了承ください。

太字 イタリック アンダーライン ハイパーリンク 引用
[サインインしない場合はここにCAPTCHAを表示します]

コメント投稿後にScript Errorや500エラーが表示された場合は、すぐに再送信せず、ブラウザの「戻る」ボタンで一旦エントリーのページに戻り(プレビュー画面で投稿した場合は、投稿内容をマウスコピーしてからエントリーのページに戻り)、ブラウザをリロードして投稿コメントが反映されていることを確認してください。

コメント欄に(X)HTMLタグやMTタグを記述される場合、「<」は「&lt;」、「>」は「&gt;」と入力してください。例えば「<$MTBlogURL$>」は「&lt;$MTBlogURL$&gt;」となります(全て半角文字)

Now loading...
Introduction
Entries of this Category
Recent Entries
Recent Comments
Recent Trackbacks
QRcode

現在停止中です
携帯電話からこのQRcodeを撮影することで携帯用URLを取得することができます

URI for cellular phones
ギターに入った猫
Styles
Font Size
Default
For defective color vision
Gray Scale
RGB Color
Search this site

このブログをメールで購読する by:FeedBurner

loading ...
Categories
Monthly Archives
BlogPeople
Syndicate this site
FeedBurner(RSS1.0/RSS2.0/Atom)
Counter
これまでのアクセス
クリエイティブ・コモンズ・ライセンス
Powered by
Movable Type 5.12