How I use Markdown with WordPress

I love writing stuff in Markdown. But when I decided to write my blog posts in MD I wanted to have the same syntax as GitHub. In particular fenced code blocks.

There are a lot of plugins for WordPress to use MD, but most of them were too complicated for my liking. And they didn’t support GitHub’s syntax. So I ended up writing a small Rack app with Sinatra that uses Redcarpet to parse my blog posts.

The rundown

  1. When I save a post, WP sends a POST request to my Heroku app.
  2. Heroku parses the data and returns it to WP
  3. WP saves the parsed MD into post_content and the raw MD into post_content_filtered
  4. When I edit a post, WP grabs the content from post_content_filtered instead of post_content

The Rack App

I’ve uploaded my Rack app to GitHub so you can see the whole thing, but here’s the important stuff.

As you can see the code is dead simple. It just grabs the content passed in $_POST['markdown'] (from a PHP perspective) and returns the rendered Markdown.

Modifying WordPress

All we do is override the functionality of WP to parse the post content before it is saved and grab the MD when we edit a post.

The code probably needs to be put into a plugin so it works with all themes, but I put it into my functions.php file. It’s also important to note that the PHP is based on Mark Jaquith’s Markdown on Save.

  • ha

    checking this thing..

  • ha

    thank you for your guides, please post some more php easier things for begginers! i liked your guide about php classes in youtube, thank you.

%d bloggers like this: