How to Add Default Content for Posts in WordPress

How to Add Default Content for Posts in WordPress

Sometimes you need to make sure that all your WordPress posts and pages have a certain “default” content attached to them. This can be something like a disclaimer, or a warning. For example if you routinely review books and movies, you might want to add a “spoiler warning” to the top of your posts. Similarly, copyright notices can be placed at the bottom of regular posts. If you’re covering an event, you might want to inform readers that the content might be outdated.

Whatever the specifics, there is a real need to have default content in different WordPress posts. In this article, we’ll take a look at the best way to achieve this via scalable filters.

Getting the HTML of your Content

In the screenshot below, I’ve created an example of default content on my blog. It’s a standard type of copyright notice with italics and bold text.

copyright notice added

What we want to get, is the underlying HTML so we can plug it into our custom function. To do this, click on the “Text” tab in the WordPress editor to reveal the raw text.

Here, you can see the <strong> and <em> HTML tags that were used to create the final effect. We’ll be using these in the next section.

Inserting the Default Content

The code below inserts default content into the WordPress editor at the time of post creation. This way, it’s up to the writer to decide where they want to place it. It also lets them make appropriate modifications and decide on the exact placement for each post.

Open up your functions.php file and paste the following code:

function insert_default_content($content) {
$content = 'All rights, including <em>this very important stuff here</em>, in the content of these web pages are owned or controlled for these purposes by the <strong>xyz blah blah corporation</strong>.';
return $content;
}
add_filter( 'default_content', 'insert_default_content');

If you haven’t added code to WordPress before in functions.php, here’s a quick tutorial on how to do this.

Replace the section in bold with your own default content. This hooks into the “default_content” filter which passes the content to the post editor whenever a new post is created. As mentioned above, this allows for greater flexibility in customizing the content as well as the placement of the default content.

However if this is always going to be a static message and has a fixed location in the post, we can use yet another function to make things even simpler. Take the following code for example:

function insert_default_content($content) {
$content = $content . 'All rights, including <em>this very important stuff here</em>, in the content of these web pages are owned or controlled for these purposes by the <strong>xyz blah blah corporation</strong>.';
return $content;
}
add_filter( 'the_content', 'insert_default_content');

This uses the “the_content” filter instead. It appends the text in bold to the end of the regular post content. So for example, if I’ve created a post that looks like this:

end of post

The final output when displayed on the screen will look like this:

content added underneath

This way, the writers are completely unaware of the additional post content. By manipulating the string operation, you can make the notice appear above or below the post content. You can even have a conditional operator to check for the type of post and insert different types of content depending on whether it’s a review, a news item, a blog post etc.

You can see the tremendous flexibility we have when it comes to default post content. You can set it up exactly the way you want – either with or without modifications. Use the code and technique that suits you best!

One Reply to “How to Add Default Content for Posts in WordPress”!

  1. Thanks – good information on adding a default text to the wordpress post wasn’t easy to find. We ued it on http://www.lgfa92.co.uk and it’s far better with our new posts, it’s easier than forgetting to add the text and having to go back and update the post.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.