(Translated by https://www.hiragana.jp/)
WordPress 3.0にMovable Typeのタグをインポートする: 小粋空間
WordPress 3.0にMovable Typeのタグをインポートする

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

Posted at July 20,2010 1:33 AM
Tag:[Customize, Import, MovableType, WordPress]
  • Hatena ブックマーク
  • del.icio.us
  • livedoor
  • Google Bookmarks
  • Yahoo!ブックマーク
  • POOKMARK Airlines
  • ニフティクリップ
  • Buzzurl
  • newsing it!

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修正しゅうせいのソースコードに修正しゅうせいもれがあったので、追記ついきしました。

関連かんれん記事きじ
トラックバック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
コメントする
コメントするにはまずサインインしてください。
Loading...