Title: the_content
Published: April 25, 2014
Last modified: February 24, 2026

---

# apply_filters( ‘the_content’, string $content )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#more-information)
    - [Usage](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#usage)
 * [Source](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#wp--skip-link--target)

Filters the post content.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#parameters)󠁿

 `$content`string

Content of the current post.

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#more-information)󠁿

This filter is used to filter the content of a post after it is retrieved from the
database and before it is printed to the screen.

### 󠀁[Usage](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#usage)󠁿

When using this filter it’s important to check if you’re filtering the content in
the main query with the conditionals [is_main_query()](https://developer.wordpress.org/reference/functions/is_main_query/)
and [in_the_loop()](https://developer.wordpress.org/reference/functions/in_the_loop/).
The main post query can be thought of as the primary post loop that displays the
main content for a post, page or archive. Without these conditionals you could unintentionally
be filtering the content for custom loops in sidebars, footers, or elsewhere.

    ```php
    add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 1 );

    function filter_the_content_in_the_main_loop( $content ) {

    // Check if we're inside the main loop in a single Post.
    if ( is_singular() && in_the_loop() && is_main_query() ) {
    return $content . esc_html__( 'I’m filtering the content inside the main loop', 'wporg');
    }

    return $content;
    }
    ```

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#source)󠁿

    ```php
    $content = apply_filters( 'the_content', $content );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post-template.php#L256)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post-template.php#L256-L256)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#related)󠁿

| Used by | Description | 
| [insert_hooked_blocks_into_rest_response()](https://developer.wordpress.org/reference/functions/insert_hooked_blocks_into_rest_response/)`wp-includes/blocks.php` |

Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.

  | 
| [WP_REST_Revisions_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_revisions_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php` |

Prepares the revision for the REST response.

  | 
| [WP_REST_Attachments_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Prepares a single attachment output for response.

  | 
| [WP_REST_Posts_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Prepares a single post output for response.

  | 
| [wp_trim_excerpt()](https://developer.wordpress.org/reference/functions/wp_trim_excerpt/)`wp-includes/formatting.php` |

Generates an excerpt from the content, if needed.

  | 
| [get_the_content_feed()](https://developer.wordpress.org/reference/functions/get_the_content_feed/)`wp-includes/feed.php` |

Retrieves the post content for feeds.

  | 
| [the_content()](https://developer.wordpress.org/reference/functions/the_content/)`wp-includes/post-template.php` |

Displays the post content.

  | 
| [do_trackbacks()](https://developer.wordpress.org/reference/functions/do_trackbacks/)`wp-includes/comment.php` |

Performs trackbacks.

  |

[Show 3 more](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#changelog)󠁿

| Version | Description | 
| [0.71](https://developer.wordpress.org/reference/since/0.71/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 11 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-1153)
 2.    [Benjamin Intal](https://profiles.wordpress.org/bfintal/)  [  10 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-1153)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-1153)
     Vote results for this note: 6[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-1153)
 4.  Note that `the_content` filter isn’t used when `get_the_content()` function is
     called.
 5.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D1153%23feedback-editor-1153)
 6.   [Skip to note 12 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-1669)
 7.    [jon](https://profiles.wordpress.org/adiant/)  [  10 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-1669)
 8.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-1669)
     Vote results for this note: 6[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-1669)
 9.  You can choose whether $content will have Shortcodes processed before or after
     your `the_content` function has executed by setting the Priority in `add_filter`.
     The default (10) will process Shortcodes after you have returned $content at the
     end of your Filter function. A large figure, such as 99, will already have processed
     Shortcodes before your Filter function is passed $content.
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D1669%23feedback-editor-1669)
 11.  [Skip to note 13 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-2360)
 12.   [Barrett Golding](https://profiles.wordpress.org/hearvox/)  [  9 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-2360)
 13. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-2360)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-2360)
 14. **Example**
 15.     ```php
         /* Add a paragraph only to Pages. */
         function my_added_page_content ( $content ) {
             if ( is_page() ) {
                 return $content . '<p>Your content added to all pages (not posts).</p>';
             }
     
             return $content;
         }
         add_filter( 'the_content', 'my_added_page_content');
         ```
     
 16.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D2360%23feedback-editor-2360)
 17.  [Skip to note 14 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-2502)
 18.   [SeanM88](https://profiles.wordpress.org/seanm88/)  [  8 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-2502)
 19. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-2502)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-2502)
 20. **Consolidate multiple string replacement filters**
      String replacements are a
     common reason for filtering `the_content` and with **PHP7** you can now consolidate
     multiple callback functions using `preg_replace_callback_array();` if it makes
     sense to.
 21. **Example:**
 22.     ```php
         add_filter( 'the_content', 'multiple_string_replacements');
         function multiple_string_replacements ( $content ) {
         	if ( is_single() && in_the_loop() && is_main_query() ) {
         		return preg_replace_callback_array([
         			// 1. Removes WordPress injected <p> tags surrounding images in post content.
         			'/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU' => function ( &$matches ) {
         				return $matches[1] . $matches[2] . $matches[3];
         			},
         			// 2. Adds custom data-attribute to <p> tags providing a paragraph id number.
         			'|<p>|' => function ( &$matches ) {
         				static $i = 1;
         				return sprintf( '<p data-p-id="%d">', $i++ );
         			},
         		], $content );
         	}
         	return $content;
         }
         ```
     
 23.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D2502%23feedback-editor-2502)
 24.  [Skip to note 15 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-3148)
 25.   [arena](https://profiles.wordpress.org/arena/)  [  7 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-3148)
 26. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-3148)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-3148)
 27. the info at the bottom of the hook is wrong for most of the hooks :
 28. **Used by 7 functions | Uses 0 functions** (At least it could be 0 function … 
     ah ah ah !)
 29. HERE is what i have for WP 5.1 :
      * (7 is right, 0 is wrong) * and i am not counting
     apply_filters_ref_array (happily there is no for “the_content” )
 30. **apply_filter**
      the_content comment.php 2656 the_content feed.php 191 the_content
     formatting.php 3692 the_content post-template.php 247 the_content class-wp-rest-
     attachments-controller.php 310 the_content class-wp-rest-posts-controller.php 
     1532 the_content class-wp-rest-revisions-controller.php 545 **add_filter** the_content
     blocks.php 269 the_content blocks.php 296 the_content class-wp-embed.php 32 the_content
     class-wp-embed.php 39 the_content default-filters.php 142 the_content default-
     filters.php 172 the_content default-filters.php 173 the_content default-filters.
     php 174 the_content default-filters.php 175 the_content default-filters.php 176
     the_content default-filters.php 177 the_content default-filters.php 178 the_content
     default-filters.php 519
 31. There should be an indication of the WP version.
 32. my doc (WP 5.1) is available here ([https://blog.mailpress.org/hooks/wordpress-5-1/](https://blog.mailpress.org/hooks/wordpress-5-1/))!
 33.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D3148%23feedback-editor-3148)
 34.  [Skip to note 16 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-4742)
 35.   [dch2018](https://profiles.wordpress.org/dch2018/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-4742)
 36. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-4742)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-4742)
 37. This is also a good hook to use if you want to display custom fields from a plugin(
     including ACF fields).
 38. The following example is from Jeff Starr’s plugins course.
 39.     ```php
         // display all custom fields for each post
         function wpdocs_display_all_custom_fields( $content ) {
             $custom_fields = 'Custom Fields';
             $all_custom_fields = get_post_custom();
     
             foreach ( $all_custom_fields as $key => $array ) {
                 foreach ( $array as $value ) {
                     if ( '_' !== substr( $key, 0, 1 ) ) {
                         $custom_fields .= ''. $key .' =&gt; '. $value .'';
                     }
                 }
             }
     
             return $content . $custom_fields;
         }
         add_filter( 'the_content', 'wpdocs_display_all_custom_fields' );
         ```
     
 40.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D4742%23feedback-editor-4742)
 41.  [Skip to note 17 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-5125)
 42.   [Muhammad Ayoub](https://profiles.wordpress.org/mohammadayoub96/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-5125)
 43. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-5125)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-5125)
 44. Using the example to add classes to `p` tag & `h2` tag.
 45.     ```php
         <?php
         function wpdocs_replace_content( $text_content ) {
         	if ( is_page() ) {
         		$text = array(
         			'<p>' => '<p class="text-danger">',
         			'<h2>' => '<h2 class="h2">',
         		);
     
         		$text_content = str_ireplace( array_keys( $text ), $text, $text_content );
         	}
     
         	return $text_content;
         }
         add_filter( 'the_content', 'wpdocs_replace_content' );
         ?>
         ```
     
 46.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D5125%23feedback-editor-5125)
 47.  [Skip to note 18 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-5363)
 48.   [Chigozie Orunta](https://profiles.wordpress.org/chigozieorunta/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-5363)
 49. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-5363)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-5363)
 50. A useful hook to filter out vulgar words from your site and keep things nice and
     tidy for your site visitors.
 51.     ```php
         add_filter( 'the_content', 'wpdocs_filter_vulgar_words' );
     
         function wpdocs_filter_vulgar_words( $content ) {
         	$vulgar_words = array( $vulgar_word1, $vulgar_word2, $vulgar_word3, $vulgar_word4 );
     
         	foreach ( $vulgar_words as $word ) {
         		$hashed_word = substr( $word, 0, 1 ) . str_repeat( '*', strlen( $word ) - 1 );
         		$content = str_replace( $word, $hashed_word, $content );
         	}
     
         	return $content;
         }
         ```
     
 52.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D5363%23feedback-editor-5363)
 53.  [Skip to note 19 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-3918)
 54.   [trebitzki](https://profiles.wordpress.org/trebitzki/)  [  6 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-3918)
 55. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-3918)
     Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-3918)
 56. The usage example is misleading. Unless you’re _not_ interested in pages, it should
     be
 57.     ```php
         if ( ( is_single() || is_page() ) && in_the_loop() && is_main_query() ) {
         ...
         }
         ```
     
 58. You should also add a sufficiently high priority to the filter to hook on `the_content`
     before rendering happens:
 59.     ```php
         add_filter( 'the_content', 'filter_the_content_in_the_main_loop', -1 );
     
         function filter_the_content_in_the_main_loop( $content ) {
             // Check if we're inside the main loop in a post or page.
             if ( ( is_single() || is_page() ) && in_the_loop() && is_main_query() ) {
                 return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
             }
             return $content;
         }
         ```
     
 60. This should go in functions.php.
      It took me half a day to figure out why my filter
     was doing nothing.
 61.  * Thanks, the example was edited accordingly.
      * [Jb Audras](https://profiles.wordpress.org/audrasjb/) [6 years ago](https://developer.wordpress.org/reference/hooks/the_content/#comment-3928)
      * The usage example is NOT misleading. If condition is checking for [is_singular()](https://developer.wordpress.org/reference/functions/is_singular/)
        which is true for post, page, custom post type or attachment ([doc](https://developer.wordpress.org/reference/functions/is_singular/)).
        Your suggestion to use ([is_single()](https://developer.wordpress.org/reference/functions/is_single/)
        || [is_page()](https://developer.wordpress.org/reference/functions/is_page/))
        instead of [is_singular()](https://developer.wordpress.org/reference/functions/is_singular/)
        is not optimal. To your second though about higher priority to the filer in
        order to be applied before page is rendered. I guess you had other callbacks
        bind to ‘the_content’ hook with higher priority than yours (probably, you had
        a default 10) which overwrote your changes and it seemed to you that the page
        was rendered before your filter was applied, which again is a wrong perception
        of what ‘the_content’ hook is doing. All what it does is written on the top
        of this page: “This filter is used to filter the content of a post after it
        is retrieved from the database and **before** it is printed to the screen.”
        There is no way how this filter can be applied **after** the page is rendered.
      * [fylh](https://profiles.wordpress.org/fylh/) [3 years ago](https://developer.wordpress.org/reference/hooks/the_content/#comment-6389)
 62.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D3918%23feedback-editor-3918)
 63.  [Skip to note 20 content](https://developer.wordpress.org/reference/hooks/the_content/?output_format=md#comment-content-4593)
 64.   [renanbessa](https://profiles.wordpress.org/renanbessa/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/the_content/#comment-4593)
 65. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-4593)
     Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%23comment-4593)
 66. Using the example below verifies that if the ALT attribute of any image inserted
     in the content is empty, get the file name.
 67.     ```php
         function wpdocs_replaceALT( $content ) {
     
         	if ( is_single() && in_the_loop() && is_main_query() ) {
     
         		libxml_use_internal_errors( true );
     
         		$post = new DOMDocument();
         		$post->loadHTML( $content );
     
         		$images = $post->getElementsByTagName( 'img' );
     
         		foreach ( $images as $image ) {
     
         			if ( empty( $image->getAttribute( 'alt' ) ) ) {
     
         				$src = $image->getAttribute( 'src' );
         				$alt = pathinfo( $src, PATHINFO_FILENAME );
     
         				$image->setAttribute( 'alt', $alt );
     
         			}
         		}
     
         		$content = $post->saveHTML();
     
         		return $content;
         	}
         }
         add_filter( 'the_content', 'wpdocs_replaceALT' );
         ```
     
 68.  * The note will not work with content with accent.. there is a problem with encoding..
        should use :
      *     ```php
            $post->loadHTML( '<?xml encoding="utf-8" ?>' . $content );
            ```
        
      * [keusta](https://profiles.wordpress.org/keusta/) [5 years ago](https://developer.wordpress.org/reference/hooks/the_content/#comment-5098)
 69.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F%3Freplytocom%3D4593%23feedback-editor-4593)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fthe_content%2F)
before being able to contribute a note or feedback.