Add Content at the Begining of the Existing Content in WordPress Post

WordPress allows custom filter function to add additional content to the beginning of the existing content from your “function.php” file without writing anything directly in the “single.php” file or “page.php” file.

Add the following code in your function.php file in the theme folder

function add_custom_content_to_the_content( $content ) {
    $custom_content = 'YOUR CONTENT GOES HERE';
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'add_custom_content_to_the_content' );

Replace ‘YOUR CONTENT GOES HERE’ with your custom content, so that it will be displayed at the beginning of the content. It will be good for those who are trying to add an advertisement script on the content page.

If you want the new content only in the single post page use the condition “is_single()” inside the function see the code

function add_custom_content_to_the_content( $content ) {
 if ( is_single() ) {
    $custom_content = 'YOUR CONTENT GOES HERE';
    $custom_content .= $content;
    return $custom_content;
 } else {
    return $content;
 }
}
add_filter( 'the_content', 'add_custom_content_to_the_content' );

Leave a Comment