This implements the WordPress XML RPC API as released in version 3.4.
WARNING: SSL is NOT enabled by default for ease of testing for those running OS X systems without setup SSL certs. If this is important to you, checkout the options for instantiating a new client where you can set :use_ssl to true.
gem install rubypress
# Add this to your Gemfile
gem 'rubypress'
require 'rubypress'
wp = Rubypress::Client.new(:host => "yourwordpresssite.com",
:username => "yourwordpressuser@wordpress.com",
:password => "yourwordpresspassword")
When creating the client, you can optionally pass :retry_timeouts => true
to rescue Timeout::Error and Net::ReadTimeout errors and retry the call.
wp = Rubypress::Client.new(:host => "yourwordpresssite.com",
:username => "yourwordpressuser@wordpress.com",
:password => "yourwordpresspassword",
:retry_timeouts => true)
xmlrpc.php
locationNOTE: If your xmlrpc.php
is not on the host root directory, you need to
specify it's path. For example, to connect to myhostedwordpresssite.net/path/to/blog
:
wp = Rubypress::Client.new(:host => "myhostedwordpresssite.net",
:username => "yourwordpressuser@wordpress.com",
:password => "yourwordpresspassword",
:path => "/path/to/blog/xmlrpc.php")
(Based off of the WordPress XML RPC API Documentation)
wp.getOptions
# Returns a hash of options from the wp_options table
=> {"software_name"=>{"desc"=>"Software Name",
"readonly"=>true,
"value"=>"WordPress"}}
(just a small excerpt of actual options for the sake of the whole brevity thing)
wp.newPost( :blog_id => "your_blog_id", # 0 unless using WP Multi-Site, then use the blog id
:content => {
:post_status => "publish",
:post_date => Time.now,
:post_content => "This is the body",
:post_title => "RubyPress is the best!",
:post_name => "/rubypress-is-the-best",
:post_author => 1, # 1 if there is only the admin user, otherwise the user's id
:terms_names => {
:category => ['Category One','Category Two','Category Three'],
:post_tag => ['Tag One','Tag Two', 'Tag Three']
}
}
)
# Returns the newly created posts ID if successful
=> "24"
Use the default SSL port of 443
wp = Rubypress::Client.new(:host => "myhostedwordpresssite.net",
:username => "yourwordpressuser@wordpress.com",
:password => "yourwordpresspassword",
:use_ssl => true)
Use a non-default ssl port of your choosing (must be setup on your server correctly)
wp = Rubypress::Client.new(:host => "myhostedwordpresssite.net",
:username => "yourwordpressuser@wordpress.com",
:password => "yourwordpresspassword",
:use_ssl => true,
:ssl_port => 995)
FILENAME='myFile.png'
wp.uploadFile(:data => {
:name => FILENAME,
:type => MIME::Types.type_for(FILENAME).first.to_s,
:bits => XMLRPC::Base64.new(IO.read(FILENAME))
})
To make further requests, check out the documentation - this gem should follow the exact format of the WordPress XML RPC API. For even further clarification on what requests are available, take a look in the spec folder.
Pull requests welcome.
The test suite requires that the following environment variables are set:
Optionally, you can create a file in the working directory called .env and add the following to it:
WORDPRESS_HOST=myhostedwordpresssite.net
WORDPRESS_PORT=80
WORDPRESS_USE_SSL=false
WORDPRESS_USERNAME=yourwordpressuser@wordpress.com
WORDPRESS_PASSWORD=yourwordpresspassword
or use the sample-dot-env file as a base. .env will not be committed. When RSpec runs it will set the environment variables for you.
If you use a port other than 80, specify it with WORDPRESS_PORT
and use WORDPRESS_USE_SSL=true
for HTTPS servers. Be sure to set
the port to 443 for standard HTTPS servers.
If you'd like to run the tests to test a server with plain HTTP authentication, use these environment vars:
WORDPRESS_HTTP_LOGIN=yourhttplogin
WORDPRESS_HTTP_PASS=yourhttppass
WORDPRESS_HTTP_USERNAME=yourwordpressusername
WORDPRESS_HTTP_PASSWORD=yourwordpresspassword
WORDPRESS_HTTP_HOST=yourhost.com
WORDPRESS_HTTP_PORT=80
WORDPRESS_HTTP_USE_SSL=false
WORDPRESS_HTTP_PATH=/path/to/xmlrpc.php
The Basic Authentication settings also allow a custom port and whether to use SSL/HTTPS. Note that, like the host and path, these
variable names include HTTP_
and can be set to the same or different values as needed.
Licensed under the same terms as WordPress itself - GPLv2.