Upgrading to Markdown on Save Improved

Last weekend I upgraded my wordpress installation to use Markdown on Save Improved, a plugin that saves two versions of each post, one that is written in markdown formatted text and another that is HTML. The latter is used by wordpress to serve pages to the viewer, and the former is used by the author to write posts. The advantage is having the markdown preprocessed before being served to viewers, thus cutting down on the total resources used by the server. (Of course, too much traffic has never really been a problem on this website.)

One problem I encountered was how to deal with the 500 some odd posts I already had, for which 60% or so were already in markdown. I needed to find a way to trigger wordpress to transform these older entries into HTML and store that HTML for display, while keeping the markdown for editing. The plugin didn’t have any of this functionality so I had to resort to SQL and PHP hacking. So fair warning to anyone who may try something similar - backup your database. I’m not responsible for any lost data.

I started by looking at the WP database using PHPMyAdmin. I wanted to change the wp-posts column to move my old markdown entries from post_content to post_content_filtered. I ended up with the following SQL statement.

UPDATE `wp-posts` SET
`post_content_filtered` = `post_content`
WHERE `post_status` = 'published'

Be careful with this statement and make sure you put the destination_column first or else your entries may be erased. I tested my syntax on a single entry first to make sure I had it correct.

The next step was modifying the plugin. I did this with the built-in plugin editor in the WordPress control panel. I noticed that there was a function already present that would format old posts if they had content in the _post_content_filtered column. I wanted to make sure that this function would be triggered when I re-activated the plugin.

So I altered the public function activate to call update_schema regardless of the if statement testing for the version. To minimize the changes I needed to make in the code I just changed:

if ( version_compare( '2.1', $previous_version, '=' ) )

to

if ( TRUE )

and then reactivated the plugin. The server churned for about a minute and successfully formatted all of my old posts. Then I reverted all of my plugin changes to the original code.

Use this information at your own risk. I’m not going to do support for you.

Avatar
Todd Suomela
Associate Director for Digital Pedagogy & Scholarship Department

My interests include digital scholarship, citizen science, leadership, and communications.