{"id":1027,"date":"2009-10-26T13:14:37","date_gmt":"2009-10-26T11:14:37","guid":{"rendered":"http:\/\/www.juust.org\/?p=1027"},"modified":"2020-06-12T21:35:41","modified_gmt":"2020-06-12T19:35:41","slug":"metaweblog-newpost-posting-to-wordpress-from-word","status":"publish","type":"post","link":"https:\/\/www.juust.org\/index.php\/metaweblog-newpost-posting-to-wordpress-from-word\/2009\/10\/","title":{"rendered":"metaWeblog.newPost posting to WordPress from Word"},"content":{"rendered":"<p>Quick and dirty : posting to WordPress from Word with VBA. I put the <a href=\"https:\/\/www.juust.org\/post_to_wordpress.txt\" rel=\"nofollow\">text source<\/a> on the server. <\/p>\n<p>It works, to a point. You can take a Word document, run the PostToWordpress macro (with your own blogs settings) and it puts the content in WordPress. <\/p>\n<p>I haven&#8217;t worked with Word for a few years, you&#8217;d have to ask someone with up-to-date Word vba skills how to grab the document title, and sanitize and format the document text into proper html-output. For the tests I grabbed a snippet off the net, it does not do a perfect job but the  basic idea is kosher.<\/p>\n<h3>metaWeblog.newPost<\/h3>\n<p>Most sites (including blogger) have skipped from blogger.newPost to newer formats. I prefer using the metaWeblog.newPost rpc method, it is a bit more versatile, and supported by WordPress as well. <\/p>\n<p>(ref. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa905673.aspx\" rel=\"nofollow noopener\" target=\"_blank\">msdn library article on metaWeblog.newPost<\/a>)<\/p>\n<p>The <strong>methodcall<\/strong> is structured like this :<\/p>\n<ul>\n<li>methodname : metaWeblog.newPost<\/li>\n<li>blogid<\/li>\n<li>blog user name<\/li>\n<li>blog password<\/li>\n<li>the post struct with it&#8217;s members\n<ul>\n<li>categories array<\/li>\n<li>description (the doc text)<\/li>\n<li>title<\/li>\n<li>date created<\/li>\n<\/ul>\n<\/li>\n<li>publish (0=save as draft, 1=publish immediately)<\/li>\n<\/ul>\n<p>So let&#8217;s build that step by step as vba routine :<\/p>\n<pre lang=\"vb\">\r\n\r\nSub PostToWordpress()\r\n\r\n'the basic blog settings\r\n    txtURL = \"http:\/\/www.blog.com\/xmlrpc.php\"\r\n    txtBlogId = \"1\"\r\n    txtUserName = \"MyUserName\"\r\n    txtPassWord = \"MyPassword\"\r\n\r\n'the document settings\r\n    txtTitle = \"MyTitle\"\r\n    'note : you can use the documents creation date here\r\n    txtDateCreated = Format(Now(), \"yyyyMMdd\") & \"T\" & Format(Now(), \"hh:mm:ss\")\r\n   \r\n    'Categories is an array in the post,\r\n    'you can use category names,\r\n    'and you can always use \"Uncategorized\" \r\n    'as all wordpress blogs have it\r\n\r\n    Dim MyCategories(2) As String\r\n    MyCategories(1) = \"Uncategorized\"\r\n    MyCategories(2) = \"example_category_one\"\r\n\r\n    'note: this function grabs the document text, \r\n    'replaces linebreaks with html linebreaks etc. \r\n\r\n    'it needs a lot more work\r\n    txtDocument = getCurrentDocAsSimpleHtml()\r\n\r\n<\/pre>\n<p>Once I have the blog settings and the content + attributes for the new WordPress post, I set up the XMLHttpRequest object :<\/p>\n<pre lang=\"vb\">\r\n      \r\n    Dim objSvrHTTP As ServerXMLHTTP\r\n    Dim strT As String\r\n    Set objSvrHTTP = New ServerXMLHTTP\r\n  \r\n    objSvrHTTP.Open \"POST\", txtURL, False, CStr(txtUserName), _\r\n    CStr(txtPassWord)\r\n  \r\n    objSvrHTTP.setRequestHeader \"Accept\", \"application\/xml\"\r\n    objSvrHTTP.setRequestHeader \"Content-Type\", \"application\/xml\"\r\n<\/pre>\n<p>Then I start building the XML to send to WordPress, <\/p>\n<pre lang=\"vb\">\r\n\r\n'methodcall    \r\n    strT = strT & \"<methodcall>\"\r\n\r\n'methodname\r\n    strT = strT & \"<methodname>metaWeblog.newPost<\/methodname>\"\r\n\r\n'parameters : blog settings\r\n    strT = strT & \"<params>\"\r\n    strT = strT & \"<param \/><value><string>\" & txtBlogId & \"<\/string><\/value>\"\r\n    strT = strT & \"<param \/><value><string>\" & txtUserName & \"<\/string><\/value>\"\r\n    strT = strT & \"<param \/><value><string>\" & txtPassWord & \"<\/string><\/value>\"\r\n\r\n'parameters : the post structure\r\n    strT = strT & \"<param \/>\"\r\n        strT = strT & \"<struct>\"\r\n\r\n'parameters : post : category array\r\n            strT = strT & \"<member><name>categories<\/name>\"\r\n                strT = strT & \"<value>\"\r\n                    strT = strT & \"<array>\"\r\n                        strT = strT & \"<data>\"\r\n\r\n   For i = 1 To UBound(MyCategories)\r\n                            strT = strT & \"<value>\" & MyCategories(i) & \"<\/value>\"\r\n   Next i\r\n                        strT = strT & \"<\/data>\"\r\n                    strT = strT & \"<\/array>\"\r\n                strT = strT & \"<\/value>\"\r\n            strT = strT & \"<\/member>\"\r\n\r\n'parameters : post : the content\r\n        strT = strT & \"<member><name>description<\/name>\"\r\n            strT = strT & \"<value>< [!CDATA[\" &#038; txtDocument &#038; \"]]><\/value>\"\r\n        strT = strT & \"<\/member>\"\r\n\r\n'parameters : post : title\r\n        strT = strT & \"<member><name>title<\/name><value>\" & txtTitle & \"<\/value><\/member>\"\r\n\r\n'parameters : post : date created\r\n        strT = strT & \"<member><name>dateCreated<\/name><value><datetime .iso8601>\" &  txtDateCreated & \"<\/datetime><\/value><\/member>\"\r\n        strT = strT & \"<\/struct>\"\r\n    strT = strT & \"\"\r\n\r\n'parameters : post : store as draft (0) or publish immediately (1)\r\n    strT = strT & \"<param \/><value><boolean>0<\/boolean><\/value>\"\r\n\r\n'end parameters\r\n    strT = strT & \"<\/params>\"\r\n\r\n'end methodcall\r\n    strT = strT & \"<\/methodcall>\"\r\n\r\n'send it to wordpress\r\n    objSvrHTTP.send strT\r\n\r\n'send the response to the debug window\r\n    Debug.Print objSvrHTTP.responseText\r\n\r\nEnd Sub\r\n<\/pre>\n<h3>conversion to html<\/h3>\n<p>The main practical problem is converting the Word document content to html. I grabbed two quick functions to select the current document text, convert the most common html entities and replace linebreaks with their html counterparts, but it is far from perfect. <\/p>\n<pre lang=\"vb\">\r\nPrivate Function getCurrentDocAsSimpleHtml() As String\r\n'... see source text\r\nEnd Function\r\n\r\n' simple HTML entity encoder\r\nPrivate Function encode(ByVal s As String) As String\r\n    If s = \"&\" Then\r\n        encode = \"&amp;\"\r\n    ElseIf s = \"< \" Then\r\n        encode = \"&lt;\"\r\n    ElseIf s = \">\" Then\r\n        encode = \"&gt;\"\r\n    ElseIf s = Chr(13) Then\r\n        encode = \"<br \/>\"\r\n    Else\r\n        encode = s\r\n    End If\r\nEnd Function\r\n<\/pre>\n<p>The WordPress Incutio xml-parser will not pass content it cannot make sense of, which leaves you with an empty post.<\/p>\n<h3>using CDATA<\/h3>\n<p>A rather common hack I used above is using < ![CDATA[ ]]> to wrap the post content, which indicates to the wordpress xml-parser that I pass a string of raw character data as content. The parser ignores it, and WordPress stuffs everything in the database. <\/p>\n<p>Works great, but it can backfire and slip stuff in the database that wordpress cannot handle and display correctly. <\/p>\n<p>I&#8217;d test without using CDATA if you want to post from Word on a regular basis, or use a more professional html-converter on the word documents first.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick and dirty : posting to WordPress from Word with VBA. I put the text source on the server. It works, to a point. You can take a Word document, run the PostToWordpress macro (with your own blogs settings) and it puts the content in WordPress. I haven&#8217;t worked with Word for a few years, [&hellip;]<\/p>\n","protected":false},"author":5796,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13,22],"tags":[],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-wordpress","category-xml-rpc"],"_links":{"self":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/users\/5796"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/comments?post=1027"}],"version-history":[{"count":0,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts\/1027\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/media?parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/categories?post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/tags?post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}