Publish to WordPress with Emacs 24 and org2blog

I’ve recently discovered the absolute joy that is writing and publishing wordpress blog posts using Emacs 24 and org2blog. Because it took me a while to get everything (including source code syntax highlighting by the WordPress SyntaxHighlighter plugin) going, I wanted to document the whole procedure step-by-step, using org2blog of course!

I’m using Emacs 24.3 (from the PPA) with Prelude on Ubuntu 12.04.4.

Installing required packages

org-mode is already installed in Emacs 24 with Prelude. However, we need to install org2blog and some extra dependencies.

M-x package-install RET xml-rpc
M-x package-install RET metaweblog
M-x package-install RET org2blog
M-x package-install RET htmlize

metaweblog and xml-rpc are two direct dependencies of org2blog. htmlize is to support the syntax highlighting of source code by SyntaxHighlighter on WordPress.

Configuring Emacs

I added the following to my ~/.emacs.d/init.el. Read the comments, copy and modify!

Note the org2blog/wp-use-sourcecode-shortcode variable: If you set this to nil, Emacs and org2blog will pre-colour your source code blocks in HTML and upload that. If you set it to =’t=, org2blog will make use of the SyntaxHighlighter Evolved shortcodes, meaning that the WordPress plugin, if installed and configured correctly as shown in the next section, will be used to syntax highlight.

Option 1 is the easiest, but the colours in option 2 are often more readable. If you’re taking option 2, make sure that your org2blog is newer than 2014-05-26, as there was a bug in the HTML encoding of certain characters.

;; default is ~/org/ — I’m putting orgmode’s default dir with my other notes
(setq org-directory "~/sync/notes/org/")
;; and you need this, else you’ll get symbol void errors when doing
;; fill paragraph
(setq org-list-allow-alphabetical t)

(require ‘org2blog-autoloads)
(require ‘netrc)

;; in my ~/.netrc/ I have:
;; machine wp-cpbotha login my_username password my_password
;; machine wp-vxlabs login my_username password my_password
;; so let’s parse out each of the lines into a separate variable
(setq wp-cpbotha (netrc-machine (netrc-parse "~/.netrc") "wp-cpbotha" t))
(setq wp-vxlabs (netrc-machine (netrc-parse "~/.netrc") "wp-vxlabs" t))

;; I want to publish to both cpbotha.net and vxlabs.com
(setq org2blog/wp-blog-alist
‘(("cpbotha"
:url "http://cpbotha.net/xmlrpc.php"
:username (netrc-get wp-cpbotha "login")
:password (netrc-get wp-cpbotha "password")
:default-title "Hello World"
:default-categories ("org2blog" "emacs")
:tags-as-categories nil)
("vxlabs"
:url "http://vxlabs.com/xmlrpc.php"
:username (netrc-get wp-vxlabs "login")
:password (netrc-get wp-vxlabs "password")
:default-title "Hello World"
:default-categories ("org2blog" "emacs"))))

;; http://blog.binchen.org/posts/how-to-use-org2blog-effectively-as-a-programmer.html
;; has half the instructions, but was missing
;; `wp-use-sourcecode-shortcode` at the time of this writing, without
;; which this does not work at all.

;; * `M-x package-install RET htmlize` is required, else you get empty
;; code blocks https://github.com/punchagan/org2blog/blob/master/org2blog.el
;; * with wp-use-sourcecode-shortcode set to ‘t, org2blog will use
;; shortcodes, and hence the SyntaxHighlighter Evolved plugin on your blog.
;; however, if you set this to nil, native Emacs highlighting will be used,
;; implemented as HTML styling. Your pick!
(setq org2blog/wp-use-sourcecode-shortcode ‘t)
;; removed light="true"
(setq org2blog/wp-sourcecode-default-params nil)
;; target language needs to be in here
(setq org2blog/wp-sourcecode-langs
‘("actionscript3" "bash" "coldfusion" "cpp" "csharp" "css" "delphi"
"erlang" "fsharp" "diff" "groovy" "javascript" "java" "javafx" "matlab"
"objc" "perl" "php" "text" "powershell" "python" "ruby" "scala" "sql"
"vb" "xml"
"sh" "emacs-lisp" "lisp" "lua"))

;; this will use emacs syntax higlighting in your #+BEGIN_SRC
;; <language> <your-code> #+END_SRC code blocks.
(setq org-src-fontify-natively t)

Optional: Configuring your WordPress to highlight lisp code

This is only necessary if you have (setq org2blog/wp-use-source-shortcode 't), i.e. you want your Syntaxhighlighter Evolved to do the highlighting.

There’s a lazy hack floating around that simply uses SyntaxHighlighter Evolved’s clojure support, but this yields results that are ever more ugly than what you see here.

Instead of that, we copy shBrushLisp.js (github) into your WP installation’s wp-content/plugins/syntaxhighlighter/ directory. Then edit syntaxhighlighter.php in that same directory, adding the following line with all the other third-party brush we_register_script lines:

wp_register_script('syntaxhighlighter-brush-lisp', plugins_url('shBrushLisp.js',__FILE__),array('syntaxhighlighter-core'),'1.0.0');

Finally, modify the brush aliases as follows:

$this->brushes = (array) apply_filters( 'syntaxhighlighter_brushes', array(
   'as3' => 'as3',
   <bunch of other aliases>,
   'lisp' => 'lisp',
   'emacs-lisp' => 'lisp',

Let’s publish!

Create a .org file. A minimal publishable file would look like this:

#+TITLE: Emacs 24, Prelude and org2blog and WordPress

Here is some text!

* Heading

Some more text!

Let's include an image:

[[./img/screenie-org2blog.png]]

Login to your wordpress installation with M-x org2blog/wp-login.

Before posting this to your blog, you can get a quick preview by doing C-c C-e h o or more verbosely M-x org-html-export-as-html. (On my setup, I hit the invalid function: org-with-silent-modifications bug at this point. To fix this, temporarily remove ANY org-mode config settings from your init.el, restart Emacs, then do a package-install package org to get the latest greatest, and then put back your temporarily removed configuration. See this post for ever so slightly more detail.)

You can now publish your post with M-x org2blog/wp-post-buffer, which will upload the post as a draft to your weblog. You’ll have the option of previewing the post via your WordPress site, and then using the usual WordPress mechanism for publishing that post. You can also post and publish directly from org2blog using M-x org2blog/wp-post-buffer-and-publish (or C-c p).

When you link to an image, that image will be uploaded to the WordPress media library and inserted into the post. BONUS! Here’s a screenshot of my Emacs editing this post:

screenie-org2blog.png

11 thoughts on “Publish to WordPress with Emacs 24 and org2blog”

  1. To my mind, “absolute joy” would be the ability to post to a WordPress blog a document written in LaTeX markup, without the necessity of manual conversion to HTML.

    I use the combination Linux+Emacs+LaTeX for everything I write, entering LaTeX markup as I compose. Experience demonstrates that it is much better to utilize LaTeX markup during original composition — beginning with informal notes — than it is to add LaTeX markup to a document which is substantially complete.

    The markup provides typesetting apparatus such as bulleted lists and enumerated lists, italics, boldface, and underline, all of which facilitate the “compose-read-rewrite-read-…” cycle. Of course, this assumes that generation of typeset copy, whether on the screen or on paper, is a trivial matter; and indeed, this is the case in the Linux environment.

    I typically compose in the X environment (X is the Linux GUI), with several windows open. The windows provide immediate access (via ALT-TAB) to Emacs, a terminal, a dictionary with thesaurus, and an display (via “xdvi”) of the typeset document. After working a bit in the Emacs window, I save the document and then switch to the terminal window, in order to execute LaTeX on the revised copy. Then I switch to the xdvi window to see the typeset result of my revision. Inasmuch as xdvi automatically updates the document each time the document is compiled by LaTeX, this whole process requires only a few keystrokes:

    (1) CTR-x-s in the Emacs window saves the document, (2) ALT-TAB moves to the terminal window, (3) UPARROW + ENTER in the terminal window invokes the LaTeX command previously executed by the terminal, (4) ALT-TAB moves to the xdvi window, (5) ALT-TAB cycles back to the Emacs window for further editing.

    The entire process — from typing an edit into Emacs to viewing the resulting typeset document — takes only a few seconds. xdvi allows the document to be displayed at any desired magnification; changes in magnification are instantaneous.

    A minimal LaTeX document requires only three lines of “overhead”; for example:

    \documentclass{article}
    \begin{document}
    Body of the document.
    \end{document}

    Consequently, even a “throw-away” document such as a grocery list can be composed, typeset, and printed with minimal effort:

    \documentclass{article}
    \begin{document}
    Grocery List for 16 November 2014
    \begin{itemize}
    \item eggs
    \item milk
    \item bread
    \item bananas
    \end{itemize}
    FINIS
    \end{document}

    Is this not more simple and transparent than is the equivalent HTML markup?

    For me, the greatest impediment to blogging is the burden of conversion to HTML markup.

    1. With this org2blog setup, I only see org mode markup and absolutely no HTML. I compose in emacs in org mode markup, which is perhaps even more economical and to the point than LaTeX (for this purpose), and then post the buffer to my blog.

      I’ve written thousands of pages of LaTeX in my life, but one has to remember that LaTeX was meant for print media and not necessarily for online publishing. Blogging via org mode is a good compromise between the economy of a human-facing markup language and the requirements for good web-publishable documents.

      That being said: If you’re interested, you could build a LaTeX -> blogging setup using emacs, based on the org2blog mechanism, but I would give org2blog a shot first. 🙂

  2. i use emacs 24.5 ( org mod 8 ) with win 8 and set it like you, but it still doesn’t work well for me.
    1. install packages:
    M-x package-install RET xml-rpc
    M-x package-install RET metaweblog
    M-x package-install RET org2blog
    M-x package-install RET htmlize
    2. my emacs.d is this:https://github.com/zhizunbao84/emacs.d
    3. SyntaxHighlighter 3.2.1 plugin is installed in my wordpress 4.4.2 with the default setting.

  3. I noticed you’re using netrc package instead of auth-source mentioned in the official documentation.
    – Why did you use netrc or what’s the difference between those two packages?
    – Does netrc support encrypted files with epa (GPG)?

    1. Hi there!

      All my hard drives are fully encrypted, either with Linux dm-crypt, or via TCG Opal (see posts on this blog), and the machines are not used by anyone else, so I have given myself permission not to encrypt my blog credentials. In other words, my .netrc is in cleartext, and I rely on the hard-drive encryption to take care of it.

Leave a Reply

Your email address will not be published. Required fields are marked *