the_content
Filters the content of a post or page before it is displayed on the front end.
add_filter( 'the_content', $callback, 10, 1 );
Description
Filters the post content, allowing integrations like WP All Import to modify content before it's displayed or saved. Developers can use this to dynamically alter post text based on custom logic or imported data, but be mindful of performance impacts on large sites.
Usage
add_filter( 'the_content', 'your_function_name', 10, 1 );
Parameters
-
$content(mixed) - This parameter contains the post's content that will be displayed.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'the_content' filter hook to modify post content.
*
* This example demonstrates how to wrap the post content in a custom div
* with a specific class, allowing for easier styling or manipulation via CSS.
*/
add_filter( 'the_content', 'my_custom_content_wrapper', 10, 1 );
/**
* Wraps the post content in a custom div.
*
* @param string $content The post content.
* @return string The modified post content.
*/
function my_custom_content_wrapper( $content ) {
// Only apply this modification to single post views and not in feeds.
if ( is_single() && ! is_feed() ) {
// Wrap the content in a div with a custom class.
$wrapped_content = '<div class="my-custom-post-wrapper">' . $content . '</div>';
return $wrapped_content;
}
// Return the original content if not a single post or if in a feed.
return $content;
}
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/wp-all-import/tokens/wpai-tokens.php:310
src/integrations/wp/tokens/wp-post-tokens.php:567
src/integrations/wp/tokens/wp-post-tokens.php:741
uncanny-automator-pro/src/integrations/wp/tokens/wp-pro-tokens.php:623
uncanny-automator-pro/src/integrations/wp/tokens/wp-pro-tokens.php:749
uncanny-automator-pro/src/integrations/wp/actions/wp-setpostcontent.php:195
$value = Automator()->utilities->automator_get_the_excerpt( $post->ID );
break;
case 'POSTCONTENT':
$value = $post->post_content;
break;
case 'POSTCONTENT_BEAUTIFIED':
$content = get_the_content( $post->ID );
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content ); // phpcs:ignore Generic.PHP.Syntax.PHPSyntax
$value = $content;
break;
case 'POSTIMAGEID':
$value = get_post_thumbnail_id( $post->ID );
break;
case 'POSTIMAGEURL':