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 and the most robust, and hence my preference, but you might prefer option 2. 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 nil)
;; 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-sourcecode-shortcode 't), i.e. you want your Syntaxhighlighter Evolved to do the highlighting.

(Personally, I prefer to let Emacs do the highlighting, and then upload the htmlized version, i.e. my org2blog/wp-use-sourcecode-shortcode is set to nil. This configuration does not require any special wordpress syntax highlighting plugins.)

There’s a lazy hack floating around that simply uses SyntaxHighlighter Evolved’s clojure support, but this yields less pretty results.

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');
1
2
3
4
5
6
<p>
  Finally, modify the brush aliases as follows:
</p>

<div class="org-src-container">
  <pre class="src src-php"><span style="color: #D0372D;">$</span><span style="color: #D0372D;">this</span><span style="color: #333333; background-color: #FFFFFF;">-&gt;</span><span style="color: #BA36A5;">brushes</span> = <span style="color: #707183;">(</span><span style="color: #6434A3;">array</span><span style="color: #707183;">)</span> apply_filters<span style="color: #707183;">(</span> <span style="color: #008000;">'syntaxhighlighter_brushes'</span>, <span style="color: #0000FF;">array</span><span style="color: #7388D6;">(</span>

‘as3’ => ‘as3’, <bunch of other aliases>, ‘lisp’ => ‘lisp’, ‘emacs-lisp’ => ‘lisp’,