WordPress the_content Filter Stack: wpautop, do_blocks, do_shortcode Order and Customisation

The the_content filter stack: what runs on your post content, and when

the_content() does not print the content as stored. It runs it through the the_content filter, where core has a chain of callbacks: blocks are rendered, shortcodes run, paragraphs are added, images get srcset, embeds become players. Your own filter on the same hook sees a different text depending on its priority, and a filter in the wrong place either misses something or breaks it.

Problem: A plugin should append related posts to news articles, a landing page post type should not get automatic paragraphs, and the plugin’s own filter sometimes sees raw shortcodes and sometimes rendered HTML.

Solution: Know the core priorities (blocks at 9, texturize and paragraphs at 10, shortcodes at 11), add your filter at a priority after the step you depend on, check the context (is_singular(), in_the_loop(), is_main_query()) and remove wpautop per post type inside a filter that runs earlier.

The core chain

In WordPress 5.x and 6.0 the main callbacks are:

  • 8: WP_Embed::run_shortcode and autoembed (the [embed] shortcode and URLs on their own line)
  • 9: do_blocks (dynamic blocks are rendered)
  • 10: wptexturize (curly quotes), wpautop (paragraphs), shortcode_unautop, prepend_attachment, wp_filter_content_tags (srcset, lazy loading)
  • 11: capital_P_dangit, do_shortcode
  • 20: convert_smilies

A filter at priority 10 or lower sees unprocessed shortcodes; at 12 or higher it sees their output. A filter that adds HTML after priority 10 adds it after wpautop, so the HTML is not wrapped in <p> tags.

Appending related posts

<?php
add_filter( 'the_content', function ( $content ) {
    if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() || ! has_category( 'news' ) ) {
        return $content;
    }

    return $content . ha_related_posts_html( get_the_ID() );
}, 20 );

The three checks matter: the_content also runs for excerpts generated from the content, in widgets, in feeds, in the REST API and for every post of a secondary loop. Without them the related posts appear in places nobody intended, or a related post’s own content shows related posts again.

No automatic paragraphs for one post type

<?php
add_filter( 'the_content', function ( $content ) {
    if ( 'landing_page' === get_post_type() ) {
        remove_filter( 'the_content', 'wpautop' );
    } elseif ( ! has_filter( 'the_content', 'wpautop' ) ) {
        add_filter( 'the_content', 'wpautop' );
    }
    return $content;
}, 0 );

The check runs at priority 0, before wpautop, and adds the filter back for other post types, which matters when one page shows several posts. Content made with the block editor needs no change: wpautop skips content that contains blocks.

Getting the processed content in code

<?php
$html = apply_filters( 'the_content', get_post_field( 'post_content', $post_id ) );

get_the_content() returns the raw content without the filters. Run apply_filters( 'the_content', ... ) only when you really need the final HTML, and be aware that it also runs every plugin’s filter, including the related posts above.

The order rules of hooks in general are explained in hook priorities and execution order.

NOTE: Do not run heavy work (remote requests, large queries) in a the_content filter without caching. The filter can run several times per page, for the main content, for an excerpt, for the Open Graph description of an SEO plugin, and each time pays the full cost.