My wife’s website is filled with photos she’s taken locally, and making sure they get up there handily is my job. Previously, she used a VB program I cobbled together to move photos from her various cameras onto the computer, then upload them to the site (lightbox style display on the Windows computer, FTP to upload the pics, etc). Unfortunately the final step, embedding the images in a post, was a quick hack (copy a link to the clipboard and paste into a new post) and that needed a fix.
So to fix it, the program now uploads the pic, and then creates a new post with the picture included, ready to edit and publish.
This magic happens via XML-RPC – which is a pre-SOAP method of making remote function calls over the Internet. Using it, the VB program can call one function and create a new post.
The steps are as follows:
- Add handler code for XML-RPC. Fortunately, the very nice xml-rpc.net takes care of that, giving us a project we can compile to create a DLL to wrap XML-RPC calling. From there, we simply add the reference and DLL to the project, and get the basics of XML-RPC with little effort.
- Decide what to call. WordPress supports several protocols (as noted on their site). In this case, I call the WordPress function metaWeblog.newPost which publishes a post with supplied text. My reason to use this? I had found some code called WordPressSharp that wrapped the WordPress functions, but it was GPL (which is always awkward if I ever decided to commercialize my code); a bit more searching found me at a site that described how to code for the metaWebblog call, and no GPL, so it was the winner!
- Call it. The code to call is pretty straightforward – in fact, I wrapped it in a function for ease of use (change userName, userPwd, and xmlrpcURL to match your site):
Imports CookComputing.XmlRpc ... Public Structure blogInfo Public title As String Public description As String End Structure Public Interface IgetCatList <CookComputing.XmlRpc.XmlRpcMethod("metaWeblog.newPost")> _ Function NewPage(ByVal blogId As Integer, ByVal strUserName As String, ByVal strPassword As String, ByVal content As blogInfo, ByVal publish As Integer) As String End Interface Private Function DoPost(ByVal title As String, ByVal post As String) As Integer Dim newBlogPost As blogInfo = Nothing newBlogPost.title = title newBlogPost.description = post Dim categories As IgetCatList = CType(XmlRpcProxyGen.Create(GetType(IgetCatList)), IgetCatList) Dim clientProtocol As XmlRpcClientProtocol = CType(categories, XmlRpcClientProtocol) Dim userName As String = "blogusername" Dim userPwd As String = "bloguserpassword" Dim xmlrpcURL As String = "http://egwebsite.com/xmlrpc.php" Dim publish As Integer = 0 clientProtocol.Url = xmlrpcURL Dim result As String = Nothing result = "" Try result = categories.NewPage(1, userName, userPwd, newBlogPost, publish) If (0 = result) Then Throw New Exception("Unknown error in message") End If MessageBox.Show("Posted to Blog successfully! Post ID : " & result.ToString) Catch ex As Exception MessageBox.Show(ex.Message) Return 0 End Try Return 1 End Function
In this case, it would assign the post to the user selected, submit it as Draft (set publish to 1 to publish immediately), and assign it to the default category. The reason I kept it simple was that most posts will need some tweaking, so just get it up there and let the Missus adjust category, publish date, etc. Of course, you can modify this function to do what you want, or even set it up for another call entirely.
And that’s it. The result is a quick addon in VB for XML-RPC, and an easy way to communicate with WordPress from another program. So if you need to tame WordPress via VB.net, I’d heartily recommend the xml-rpc.net DLL.