Liberate your Markdown posts from JetPack in WordPress


A scrap of code which I hope helps you.

Problem

You installed the WordPress JetPack plugin and wrote all your blog posts in Markdown. Now you want to remove JetPack or replace it with a better Markdown parser.

You turn off JetPack's "Write posts or pages in plain-text Markdown syntax". You click edit on a post and see the HTML version of your page. Where did the Markdown version go?

Background

When you write using JetPack's Markdown plugin, the Markdown version is stored in post_content_filtered. When you hit "publish" or "update", the page is parsed as Markdown and the HTML output is stored in post_content.

When you hit "edit", the post_content_filtered version is loaded into the editor - and the process starts again.

Solution

When you edit a post, replace the content with the filtered version, then delete the filtered version.

Place this code in your theme's functions.php.

PHP PHPfunction edit_markdown_content( $content, $id ) {
    $post = get_post( $id );
    if ( $post && ! empty( $post->post_content_filtered ) ) {
        //  Get the Markdown version
        $markdown = $post->post_content_filtered;

        //  Delete the post_content_filtered version
        global $wpdb;
        $debug = $wpdb->query(
            $wpdb->prepare(
                "UPDATE $wpdb->posts SET `post_content_filtered` = '' WHERE `wp_posts`.`ID` = %d",
                                                                                              $id
            )
        );

        //  Replace the post_content with the Markdown version
        $post->post_content = $markdown;

        //  Send it to the editor with a message saying that it was restored, along with the date of restoration
        return "<!-- Restored from post_content_filtered \n" . date("c") . "\n-->" . $post->post_content;
    }
    return $post->post_content;
}
add_filter( "edit_post_content", "edit_markdown_content", 1, 2 );

I adapted it from WP Githuber MD

Direct MySQL

If you want to automatically convert all your posts, you can edit your database directly.

MYSQL MYSQLUPDATE wp_posts
SET
    post_content = post_content_filtered,
    post_content_filtered = ''
WHERE post_content_filtered IS NOT NULL AND post_content_filtered<>'';

Warning

If you do not have a Markdown parser installed, posts will come out looking *very* strange.


Share this post on…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">