Enable Markdown Support in WordPress Without Plugins

wordpress markdown supportparsedown integrationno plugin markdownwordpress content filtermarkdown to html conversion
Published·Modified·

WordPress does not natively support Markdown markup (though it is unclear if the latest version has implemented this). While many third-party Markdown plugins are available, none have delivered satisfactory results. I have always used HTML tags to write articles; although the layout looks good, it is very laborious and inefficient. Therefore, I devised a method to allow WordPress to better support Markdown syntax without installing additional plugins.

Download Parsedown

Parsedown can parse Markdown content into HTML. If the content is already HTML, it will not be parsed. With Parsedown's support, when publishing WordPress articles, you can use both the original text mode (HTML) and Markdown syntax for writing, with neither conflicting with the other.

  1. Go to https://github.com/erusev/parsedown/releases/ to download the latest version of Parsedown.
  2. Create a new directory named extend in your theme directory.
  3. Place Parsedown.php into the extend directory.

Add Hooks

Add the following code to the functions.php file in your theme directory to register it as a WordPress hook:

// Markdown parsing
function wp_parsedown(){
	include_once(get_stylesheet_directory()."/extend/Parsedown.php");
	$Parsedown = new Parsedown();

	$content = get_the_content();
	$content = $Parsedown->text($content);
	if(is_single() || is_page()){
		echo $content;
	}
	else{
		$content = strip_tags($content);
		$content = mb_substr($content,0,180,'UTF-8');
		echo $content;
	}
}
add_action('the_content','wp_parsedown');

If you need to automatically add nofollow to external article links and open them in a new window, use the following code:

// Markdown parsing, add nofollow
function wp_parsedown(){
    include_once(get_stylesheet_directory()."/extend/Parsedown.php");
    $Parsedown = new Parsedown();

    $content = get_the_content();
    $content = $Parsedown->text($content);
    if(is_single() || is_page()){
	    preg_match_all('/href="(.*?)"/',$content,$matches);
		 if($matches){
		  foreach($matches[1] as $val){
		   if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", 
		 
			"href=\"$val\" rel=\"external nofollow\" target = \"_blank\" ",$content);
		  }
		 }
        
        echo $content;
    }
    else{
        $content = strip_tags($content);
        $content = mb_substr($content,0,180,'UTF-8');
        echo $content;
    }
}
add_action('the_content','wp_parsedown');

Note the line $content = mb_substr($content,0,180,'UTF-8'); in the code above. The number 180 represents the character count for the article summary on the homepage. Please modify it according to your specific needs.

Switch to Text Mode

WordPress text mode supports HTML writing. With the steps above, the text mode now perfectly supports Markdown syntax.

Other Notes

This method is simple to operate and requires no additional plugins, perfectly compatible with the original text mode. When writing this article, I was already using Markdown, so you can check the results.