When you write a post in text section instead of visual WordPress will work with HTML. But when you save the post and preview it, it will transform your HTML-like text into real HTML with proper paragraphs etc. Where can I find the code how WordPress does this?

I need that algorithm to transform some text from HTML into HTML of my own that's coming from WordPress posts.


The specific function you are referring to is wpautop, which adds paragraphs & HTML linebreaks to already intermixed HTML & plain text.

wpautop is part of a family of content-processing functions hooked to the_content, a filter applied to the post content after pulling from the database just before outputting. Taking a look at wp-includes/default-filters.php, you can see the default functions are:

add_filter( 'the_content', 'wptexturize'                       );
add_filter( 'the_content', 'convert_smilies'                   );
add_filter( 'the_content', 'wpautop'                           );
add_filter( 'the_content', 'shortcode_unautop'                 );
add_filter( 'the_content', 'prepend_attachment'                );
add_filter( 'the_content', 'wp_make_content_images_responsive' );

You can remove them with remove_filter( 'the_content', 'name_of_function' ), and add your own with add_filter( 'the_content', 'my_function' ); - see the documentation for more information on adding filters.

  • Perfect, code for wpautop was exactly what I was looking for!
    – Jonathan
    May 10 '16 at 11:32

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.