LoginSignup
8

More than 5 years have passed since last update.

RubyからWordPressのXML-RPC MetaWeblog APIを使って取得、投稿、編集、削除する。(Rails)

Last updated at Posted at 2014-12-04

RailsからWordPressのXML-RPCを使って投稿を追加、編集、削除する方法のメモ。そのうち、メディアアップロードなども掲載予定。

initialize

 require 'xmlrpc/client'

  def initialize(args={})
    @client = XMLRPC::Client.new( 'localhost', '/XXXXXXXXXX/xmlrpc.php')

    # ユーザ情報
    @user_name = 'username'
    @password = 'password'

    # 記事コンテンツ
    @struct = {
        title: args[:title],
        categories: args[:categories],
        description: args[:content],
        dateCreated: args[:date]
    }

    # 0: 下書き投稿, 1: 即時投稿
    @publish = args[:publish]

  end

投稿、編集、削除

  # 記事を取得します。
  def get(post_id)
    post = @client.call(
        "metaWeblog.getPost",
        post_id,
        @user_name,
        @password
    )
    post
  end

  # 記事を投稿します。
  def post
    post_id = @client.call(
        "metaWeblog.newPost",
        1,
        @user_name,
        @password,
        @struct,
        @publish
    )
    post_id
  end

  # 記事を更新します。
  def update(post_id)
    @client.call(
        "metaWeblog.editPost",
        post_id,
        @user_name,
        @password,
        @struct,
        @publish
    )
  end

  # 記事を削除します。
  #第二引数に何か文字列が入ると削除が無視されます。
  def destroy(post_id)
    @client.call(
        "metaWeblog.deletePost",
        '',
        post_id,
        @user_name,
        @password,
        @publish
    )
  end

全部

wp_post.rb

# coding: utf-8

class WpPost

  require 'xmlrpc/client'

  def initialize(args={})
    @client = XMLRPC::Client.new( 'localhost', '/XXXXXXXXXX/xmlrpc.php')

    # ユーザ情報
    @user_name = 'username'
    @password = 'password'

    # 記事コンテンツ
    @struct = {
        title: args[:title],
        categories: args[:categories],
        description: args[:content],
        dateCreated: args[:date]
    }

    # 0: 下書き投稿, 1: 即時投稿
    @publish = args[:publish]

  end

 # 記事を取得します。
  def get(post_id)
    post = @client.call(
        "metaWeblog.getPost",
        post_id,
        @user_name,
        @password
    )
    post
  end

  # 記事を投稿します。
  def post
    post_id = @client.call(
        "metaWeblog.newPost",
        1,
        @user_name,
        @password,
        @struct,
        @publish
    )
    post_id
  end

  # 記事を更新します。
  def update(post_id)
    @client.call(
        "metaWeblog.editPost",
        post_id,
        @user_name,
        @password,
        @struct,
        @publish
    )
  end

  # 記事を削除します。
  # 第二引数に何か文字列が入ると削除が無視されます。
  def destroy(post_id)
    @client.call(
        "metaWeblog.deletePost",
        '',
        post_id,
        @user_name,
        @password,
        @publish
    )
  end

end

利用

# @post.categoriesはきっと関連テーブルになってると思うので事前にcategory名だけを配列にしておきます。
post_args = {
        title: @post.title
        categories: post_categories,
        content: @post.content,
        date: @post.created_at,
        publish: 1
    }
@wp_xmlrpc = WpPost.new(post_args)

# 取得
post = @wp_xmlrpc.get(post_id)
# 投稿
post_id = @wp_xmlrpc.get
# 更新
@wp_xmlrpc.update(post_id)
# 削除
@wp_xmlrpc.destroy(post_id)

こんな感じでしょうか。とりあえずちょっと使ってみてメモです。もうちょっといい感じにできるやり方とかあったら更新していく予定です。

その後の更新

MetaWeblog APIだとカスタム投稿タイプや細かいところまでできないのでXML-RPC WordPress APIのがいいです。rubypressってgemで簡単にできます。

ソースだけ載せときます。

require 'rubypress'

  def initialize(args={})

    @username = ''
    @password = ''

    # 環境
    @wp_client = Rubypress::Client.new(
        host: '',
        path: '/xmlrpc.php',
        port: 80,
        username: @username,
        password: @password
    )

    @date = args[:date]

    # 記事コンテンツ
    @content = {
        post_status: args[:publish],
        post_title: args[:title],
        post_content: args[:content],
        post_type: args[:post_type]
    }

  end

  # 記事を取得します。
  def get(post_id)
    post = @wp_client.getPost(
        blog_id: 1,
        username: @username,
        password: @password,
        post_id: post_id
    )
    post
  end

  # 記事を更新します。
  def update(post_id)
    @wp_client.editPost(
        blog_id: 1,
        username: @username,
        password: @password,
        post_id: post_id,
        content: @content
    )
  end

  # 記事を投稿します。
  def post
    post_id = @wp_client.newPost(
        blog_id: 1,
        username: @username,
        password: @password,
        post_date: @date,
        content: @content
    )
    post_id
  end

  # 記事を削除します。
  def destroy(post_id)
    @wp_client.deletePost(
        blog_id: 1,
        username: @username,
        password: @password,
        post_id: post_id
    )
  end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8