How to use MarkDown in a custom textarea field?
I've created a custom metabox with a textarea in it. How would I go about using Markdown in this textarea? I've seen some WP plugins but they seem to only be for the main editor.
I've created a custom metabox with a textarea in it. How would I go about using Markdown in this textarea? I've seen some WP plugins but they seem to only be for the main editor.
The best Markdown parser I've found so far is the Parsedown
. Something like this:
if ( ! class_exists( 'Parsedown' ) ) {
require_once( get_template_directory() . '/Parsedown.php' );
}
$parsedown = new Parsedown();
echo $parsedown->text( $content );
If you are dealing with a client this is the way to go, Parsedown is almost bulletproof.
I installed ACF Custom Field WYSIWYG and Markdown Extra.
In functions.php
, add code:
remove_filter( 'acf_the_content', 'wpautop' );
remove_filter( 'acf_the_content', 'wptexturize' );
add_filter( 'acf_the_content', 'Markdown' );
Every custom post field that uses acf_the_content
will convert to Markdown. You can use the main content too:
add_filter( 'the_content', 'Markdown' );
You can download a copy of PHP Markdown and use it to parse the textarea contents before you save it:
if ( ! class_exists( 'Markdown' ) ) {
require_once( plugin_dir_path(__FILE__) . '/markdown.php' );
}
$textarea_contents = Markdown::defaultTransform( $textarea_contents );
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.