Add content after or before posts content automatically in wordpress, display shortcode or anything after or before post content easily and without plugin.

What is The Content Filter

Using the content filter we will add content after or before posts content automatically, and we can display shortcode after or before posts content automatically, and we can do something like removing empty paragraphs, and we can do more.

Add Content Before Post

This is an example for the content filter, in this example we will add text “Hello World” before all posts content automatically:

function WPTime_add_content_before_post($content){
    $my_text = '<h3>Hello World</h3>';
    return $my_text.$content; // add content before post
add_filter('the_content', 'WPTime_add_content_before_post');

Add Content After Post

In this example we will add word “Thanks!” after all posts content automatically:

function WPTime_add_content_after_post($content){
    $my_text = '<p>Thanks!</p>';
    return $content.$my_text; // add content after post
add_filter('the_content', 'WPTime_add_content_after_post');

Single Post Or Page

To add content in single post only, use is_single() function, for example:

function WPTime_add_content_after_post_single_only($content){
    if( is_single() ){ // if is single post
         $my_text = '<p>Thanks!</p>';
         return $content.$my_text;
    }else{ // if not single post
        return $content;
add_filter('the_content', 'WPTime_add_content_after_post_single_only');

To add content in page only, use is_page() function, to add content in both “single post and page” use is_singular() function.

Display Shortcode After Post

To display shortcode after all posts content automatically, use do_shortcode() function, for example:

function display_shortcode_after_content( $content ){
    return $content.do_shortcode('[my_shortcode]'); // display shortcode after post
add_filter('the_content', 'display_shortcode_after_content');

Display The Content In The Last

Add number of rank, for example “999”:

add_filter('the_content', 'display_shortcode_after_content', 999);

No will be display your shortcode in the last.

Another Example

We can do more with the content filter in wordpress, for example add class to all post links automatically:

function WPTime_add_class_to_links_automatically($content){
    /* Filter by Qassim Hassan - */
    $my_class = 'class="my-custom-class another-class"'; // your link class
    $add_class = str_replace('<a ', "<a $my_class ", $content); // add class
    return $add_class; // display class in links
add_filter('the_content', 'WPTime_add_class_to_links_automatically');