Justin Tadlock

How to preset text in the WordPress post editor

I came across an interesting forum post the other day titled NO ONE can answer this: Preset Text In Wordpress [sic] Posts. Of course, I took this as a personal challenge to actually figure out how this is done.

The forum thread linked to another article by a self-titled WordPress guru. I will not link to the post here because the author told people to edit a core WordPress administration file. Of course, the code was useless since a more recent version of WordPress has been released. Piece of advice: Never edit a core WordPress file.

There are built-in action and filter hooks that allow us to change things. I’ll be showing you how to use a simple filter to preset text in the WordPress post/page editor. This technique will work with both the visual and HTML editor.

Presetting content in the post editor

We’ve established that you shouldn’t edit core files. Then what should we edit? Our theme’s functions.php file. Or, we could go through the process of making a plugin, but this is pretty simple stuff.

Open functions.php in your favorite text editor and input this PHP code:

<?php

add_filter( 'default_content', 'my_editor_content' );

function my_editor_content( $content ) {

	$content = "This is some custom content I'm adding to the post editor because I hate re-typing it.";

	return $content;
}

?>

It’s as simple as that. Just a few lines of code. You could even add in some XHTML if you want.

This will only add your text to new posts. It will not add it to posts that have already been written or saved. And, you can always delete it when writing the post if it’s not needed.

Update: Hat tip to Ray for pointing out the default_content filter hook.

Final thoughts

This was the only filter hook I could find to add text. If you have a better technique, feel free to post it in the comments.

I thought about packaging this up as a plugin and giving it a pretty settings page, but that might be overkill. If enough of you want it as a plugin, I wouldn’t mind creating it. It wouldn’t really take me long to put together.

To add one extra piece of advice: If you ever come across an article on the Internet that tells you to edit a core WordPress file (anything that’s not in your /plugins or /themes directories), then stop reading that site immediately.

Like this tutorial? Please consider helping me write more in the future by making a donation via PayPal, grabbing something from my Amazon Wish List, or signing up at Theme Hybrid where you can ask me any support questions you want.