I recently had to migrate a bunch of pages from a custom CMS to a WordPress blog. Groovy to the rescue!
I used the wordpress java client library wordpress-java, but I had to checkout and build the latest version from SVN since the binary download didn’t include support for custom fields. This method will connect to a WordPress blog, retrieve a template post, upload a couple of media files, then create a new post from the template that includes links to the uploaded files. Remember to enable xmlrpc in your WordPress blog options before trying this!
def makePost(String title, String text, File image, File pdf){
// your wordpress account details go here
WordPress wp = new WordPress('username', 'password', 'http://example.com/blog/xmlrpc.php')
// make a not of the post ID of the template post
page templatePost = wp.getPost(3)
// upload the media files - an image and a pdf
// make sure to set the correct mime-types
def uploadedImage = wp.newMediaObject('image/png', image, true)
def uploadedPdf = wp.newMediaObject('application/pdf', pdf, true)
// make html
templatePost.setTitle(title)
templatePost.setDescription(
"""I am using triple quotes so I can write a multi-line string.
link to the uploaded pdf
link to the uploaded image
""")
String result = wp.newPost(templatePost, 'true')
System.out.println("new post is at ${newPost.getPermaLink()}")
}




Recent comments