WordPress Filter To Set Default Common Content In Your Post Editor?

How to add default common content in your WordPress post editor so that you don’t have to type or copy paste again and again? Most of the wordpress blog authors uses some common content across all their blog post. Common example is the “About Author” section under each of the post.  Another example of common content across all the posts is the social media share options. Every time you add a new post you need to copy and paste these common script to the post editor.
Today we will see how we can utilize WordPress Filter to add default common content in your post editor. For example you can see the “About the Author” and “Thank you for reading!” section at the bottom of this post. This is a common content across all the posts in Globinch. So I have configured this as a common text which appears when you open the Post editor every time.
WordPress allows you to write custom function that hooks to a specific filter action. There are plenty of filters are defined for different purposes including Post, Page, Attachment ,Comment, Trackback, Ping,Category ,Term etc to name a few.

How to Add Default Content in Your WordPress Post Editor

To add default content in your WordPress post editor you can make use of “add_filter” function. WordPress provides a list of predefined filters. “default_content” is one of such administrative Filter. You can attach a function to this filter and that will be applied to the default post content prior to opening the editor for a new post. That means before opening the post editor the wordpress will call this custom function to apply the “default_content” filter.

Syntax of using add_filter function to add default_content filter function

The following is the syntax of adding any add_filter function.

1
<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?>

Only the first two parameters are mandatory here. Tag which is the name of the filter to hook the $function_to_add to and function_to_add which is the custom function which will be executed.
Now you can add this filter to your functions.php file. But before adding this code please make sure that you backup your existing files, so that you can roll back the changes in case of any issues.Use at Your Own Risk

1
2
3
4
5
6
7
8
9
<?php
add_filter( 'default_content', 'addAboutAuthor' );
 
function addAboutAuthor( $content ) {
	$content =  '<div class="about-author"> 
        		<h4>About the Author</h4>'.get_avatar(get_the_author_id(), 48).' Binu George , the author of Globinch.com, is a technology blogger from India and a software solution architect by profession. </div>';
	return $content;
}
?>

Make sure that you have a CSS style class named “about-author” defined in your CSS style file.
That’s all required. Next time you open the post editor to add a new post the “About the Author” default content will be displayed in the post editor.

Before you Go,

Before you go, subscribe to get latest technology articles right in your mailbox!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Shares