I'd like to write my Posts (and other users their Comments) in plain text and let a Markdown engine run on it later. However the Editor seems to convert the text and replaces some characters when saving etc..

Is there a way to store and retrieve the plain text from the users input in the database and get it unchanged back for further manual editing?

I already disabled the visual editor. Maybe WP got some hooks running on this, that I have to disable?

I'm using this in functions.php right now:

/* * * [Markdown] * * */
$bada_md_path = get_template_directory() . '/ext/Markdown/Michelf/';

require_once( $bada_md_path . 'MarkdownExtra.inc.php' );

use Michelf\MarkdownExtra;

function bada_md_content( $content )
    $content = Michelf\MarkdownExtra::defaultTransform( $content );

    return '###</br>' . $content . '</br>###';

add_filter( 'comment_text' , 'bada_md_content' );
add_filter( 'the_content' , 'bada_md_content' );
  • You can filter the content before saving and/or before displaying it, but in your case you should think about totally disable visual editor as visual editor is for edit HTML in visual mode and you don't want it.
    – cybmeta
    Feb 16 '15 at 12:14
  • In fact I copy'n'paste the plain markdown text to the editor (the code editor, not the visual one). This input-text I want to store and retrieve in the database. When I display this text I let run a MD engine over it and edit it further. The same applies to the comments. Feb 16 '15 at 12:17
  • Can you post an example of text with characters being replaced so we can reproduce the issue?
    – cybmeta
    Feb 16 '15 at 12:33
  • Whitespace gets removed, the - (minus) sign gets replaced by another one thats not compatible etc.. Feb 16 '15 at 13:10
  • I'm sorry but I can not reproduce you issue with that information. I've written in text mode a text, minus sign and whitspaces and that characters are not replaced by any other characters.
    – cybmeta
    Feb 16 '15 at 13:16

You could refer to this:

function my_formatter($content) {
       $new_content = '';
       $pattern_full = '{(\[raw\].*?\[/raw\])}is';
       $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
       $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

       foreach ($pieces as $piece) {
               if (preg_match($pattern_contents, $piece, $matches)) {
                       $new_content .= $matches[1];
               } else {
                       $new_content .= wptexturize(wpautop($piece));

       return $new_content;

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'my_formatter', 99);

Basically, what this does it alters the existing [raw] shortcode and by wrapping your content inside this shortcode, WordPress should stop messing with your code :)

Hope this helps.

  • Thank you very much! Do you knwo where I can find all filters/modifiers/actions etc that are applied to the_content (or the comments or more general the text from a user)? Feb 16 '15 at 13:13
  • These are basically all of them :) Feb 16 '15 at 13:29
  • I removed the same filters from 'comment_text' and now everything seems to be as as I'd like it to be. Thanks again Feb 16 '15 at 13:46
  • Glad that helped you out @Serthy Feb 16 '15 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.