set featured image for WordPress post via XML-RPC

后端 未结 2 768
猫巷女王i
猫巷女王i 2021-01-21 08:31

About 6 months ago there was an update to the WordPress API which allows setting a post\'s thumbnail (or featured) image.

http://www.maxcutler.com/2012/04/04/xml-rpc-in

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-21 09:18

    I was trying to do the same with my Ruby script and using XML RPC API.

    • First Initialize and get connect to your wordpress site:

      wp = Rubypress::Client.new( :host => "your host",
          :username => "test",
          :password => "test",
          :path => "yourhost/xmlrpc.php"
        )
      
    • Upload an image which you want as featured image.

      wp.uploadFile( :data => { :name => File.basename(FILENAME),
                    :type => "image/png",
                    :bits => XMLRPC::Base64.new(File.open(FILENAME).read)
                  }
                )
      
    • get attachment id using getMediaItem method.

      attach = wp.getMediaItem(:blog_id => 0, :attachment_id => img_id.to_i)
      
      • Now create a post using newPost method

        wp.newPost( :blog_id => 0, # 0 unless using WP Multi-Site, then use the blog id
        :content => {
                     :post_status  => "draft",
                     :post_date    => Time.now,
                     :post_content => "This is the body",
                     :post_title   => "test title best!",
                     :post_name    => "test best",
                     :post_author  => 1,
                     :post_type=>'post',
                     :post_thumbnail => attach['attachment_id']
                     :terms_names  => {
                        :category   => ['Category One','Category'],
                        :post_tag => ['Tag One','Tag Two', 'Tag Three']
                                      },
        
                     }
        
        
        )
        
    • Check the result by getPost method which will return you the post

       get_data = wp.getPost(:post_id => new_post_resp.to_i, :blog_id => 0)
      

    You should Refer the following links. These all are my findings when I was facing the same issue:

    • Reference-1
    • Reference-2
    • Reference-3
    • Reference-4
    • Reference-5

提交回复
热议问题