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.
Very cooI. I assume it fires between the database and the editor display – surely someone can think of something cool to do with this… Run a translator on the text, and then run the reverse on post save to write a blog in a different language? Ok, that would be ridiculous, and probably wouldn’t work, but there’s got to be something useful to filter with this?
Peter — It fires when the editor displays. As far as running a translator on the text and publishing a new post from it, this is probably not the right filter hook. I see a few action hooks that would probably suit something like that better.
Right – you’d need to do the translation when the post was saved – you’d use this to translate back to your language when editing posts. It probably wouldn’t work very well, since translating back and forth with a translation tool usually turns into a hilarious game of telephone, but it would be interesting to come up with some filter logic that somehow was useful to the author while editing posts.
Yes, Justin – very nice.
On the same trail, what could even be better is the following.
Setting up by default Custom Fields.
On different themes, I use a certain number of Custom Fields like an extra database.
The problem is: it takes time and very often it is the same content.
What could even be better is to have, for each of the custom field, a combo by default with the last five (?) non identical entries… That would be a real life saver!
I repeat: “NO ONE can answer this: Preset Custom Fields In Wordpress”
…
I’ve been using the following (as a home-grown plugin) for over a year now. Never had to touch a core WordPress file.
I never considered putting it in functions.php. Maybe I’ll give that a try.
Gilbert — Well, I figured out how to preset custom fields, but once the post is saved, they are discarded unless you click the Update button for each custom field entry, which kind of defeats the purpose of presetting them.
The problem with setting them is that custom fields rely on the post ID, but there is no post ID until the post is created. Now, one could create a custom meta box and pre-populate it with values that would save. A good tutorial on that is Creating Custom Write Panels in WordPress.
Ray — Thanks. Using the
default_contentfilter hook is even easier than usingthe_editor_content. You don’t have to check if a post is new with it. I’m updating the post to use that instead.You can definitely add it to your theme’s
functions.phpfile. The theme functions file basically acts like a plugin that automatically loads.Hi Justin,
Coming back to my old problem of preseting custom fields.
I would be more than happy to have to click the “update” button, having then the time to potentially modify the preset values.
Went through all the filters on “http://codex.wordpress.org/Plugin_API/Filter_Reference” and I still don’t find how to do it.
Would you be so kind to describe what you found when you figured out how to preset custom fields?
A big “thank you” in advance.
Thanks for the answer Justin.
Still, having some kind of preset for custom fields would be real good for me, even if I just had to click ‘Update’ for each of them…
For example I have a website where I list all articles from the local press related to Athletics teams that I support. There are custom fields like:
[Author] -> Name of the guy who is credited for the article in the newspaper
[Date] -> Date when it was published (obviously different from the post)
[Newspaper] -> Name of the newspaper
[Newslink] -> Link to the newspaper url
[Thanks] -> A nice thank you customised with the person who told me about the article or sent me the scanned page
…
All of these fields are not really part of my personal added value in the post, and are part of the design of the page… And I can also particular content or info depending on the newspaper name.
But as you can guess a lot of these information are very often the same. I currently copy them from another post already existing… Tedious and time consuming…
Even better would be a combo box for each of these field offering an option to choose from the database one of the last unique X previous values…
I am still very interested in what you found.
Will definitely look at “Creating Custom Write Panels in WordPress” when I have five minutes.
Hey Justin, just found your website, really love it! Just want to say, keep it coming and i will follow you!
Regards,
Rodj
I suppose that i edited a core page now i can see just a blank page
Hey guys, cool tip and just what I am looking for but I can’t figure out how to get it to work. Here is my rundown.
Website: TennisConnected.com
Theme: Revolution Pro Media Theme
Wordpress 2.7.1
When our team posts an article we post an image of who the article is written by. Although this process does not take too long it is still a bit tedious. What I would like is on a new post all three picks show up in the article text box and then we just need to edit which ones aren’t needed. I tried to add the function above to the Rev Pro function.php just to see if it would work but it didn’t. Any help would be greatly appreciated. Below is an example of the final product.
http://tennisconnected.com/home/2009/04/10/jc-ferrero-contemplating-retirement/
@Dan
I don’t know if there’s a typo in your post … but the name of the file you need to drop the code into is functions.php (with an “s”) in your current theme. You said you were putting it into function.php (without the “s”).
Ray,
That was a typo. I was putting it into the functionS.php. When I create a new wordpress post, I do not see any change.
Justin –
I was intrigued by what you said about presetting custom fields, and had a look at it. It’s definitely a tricky problem, but I came up with a solution that seems to work – here’s what I did:
First, when a new post is created, WordPress creates a fake post with an ID of 0 – so, if you enter in rows in the postmeta table with a post_id of 0, they’ll show up here (I assume this is what you were talking about). At this point, I think you’re right – you have to hit “update” to get the values to stick to the new post – if you just hit save (or publish, or whatever), the values do save, but they save in their current place – with a post_id of 0. Also worth noting is that as soon as you enter a title (and click in any other input box), the post is autosaved.
If you use that initial autosave to set a marker that tells the system “This post needs default postmeta”, and then check for that on post save, you can transfer the meta from the post id 0 to your newly created post, and voila – you’ve got your default post meta.
It was a pretty interesting exercise to figure out – plenty of things went wrong on the way ( for example: Wordpress wont return anything on get_postmeta with a post id of 0, even if it exists in the database – can anybody tell me why?). Since I had the code put together an working, I threw in your default post content code, and added an admin panel, and wrapped it up as a plugin (which you can find by clicking on my name).
Anyway, thanks for the inspiration, and I’d love to hear your thoughts on how it works if you get a chance to look at it. Thanks!
Anyone know how to determine whether it’s a page or a post that’s being written?
I think I’ve figured it out. It appears for this you do need to use the original version of your function and the ‘the_editor_content’ filter, else you cannot access $post. Here is my code:
[code]
function prefill_title_tag()
{
global $post;
if( !$post || $post->ID == 0 )
{
if( $post->post_type == 'post' )
{
$default_content = "[title]";
}
}
return $default_content;
}
add_filter('the_editor_content', 'prefill_title_tag');
[/code]
hmm… the code I posted above seems to break editing-of-previous-posts-functionality.
back to the drawing board!
help me remember not to post code until I’ve tested it at least somewhat myself
Hi Justin,
thanks for this tutorial, definitely will try this out. I assume there is no way to have multiple presets?
I have another question that is similar to this. How would I do the same thing, but to add custom text to a comment box. Could I use the same code but put it in the comments themselves? I am looking for something that would allow me to let users post a comment, then after they post a comment, additional text I pre set would show up under their comment. The text would be the same, and I am just looking for a way to be able to automatically add additional text to their comment, after they have posted it a line or two under their text.
Hi,
I am very much interested in your thread. I am building my first wpmu site and would like to change the default “About” page and default text on the themes I will be using so that when a user turns on that theme the About tab is titled “Profile” and the default text is a basic outline for specific information. I wouldn’t want the outline defaulting on every page, just the one, but I like you idea of editing the functions.php.
has anyone been able to do this with success?
To do which part with success Dan? The stuff Justin mentions in the post works like a charm – are you referring to one of the ideas in the comments?
Rodjer — Thanks. I’m glad you like the site.
yasemin — I specifically said, twice said actually, in my post to not edit any core files.
Dan — If it doesn’t work, then your plugins and/or theme might be interfering with it.
Peter — You described the issue I had with custom fields perfectly. I just didn’t have the time to try and overcome the problem. I’ll check out your solution for that.
Scrib It has something to do with checking the global
$pagenowvariable forpost-new.phporpage-new.php. I haven’t really played around with it though.Profashionelle — Multiple presets based on what criteria?
mike — That’s outside the scope of this tutorial. I wouldn’t consider that presetting text, at least not in the same way. Search around for the
comment_textfilter hook (probably).ChuckS — That’s a bit outside of the scope of this article. I’ll look into it sometime. It could make for an interesting tutorial.
Dan — I know of several people using the code I’ve shown in the tutorial without any problems.
Thank you, I will try harder. So far not much help from the internet. Any chance of a tutorial coming for that any time soon?
Well, I have like 3-5 types of post of which each has its own structure. It would be such a time saver if there’d be a way to have like a drop down menu in the “write post” admin area where you can select one of the various content presets (think page templates). Do you think this is doable w/o messing around with the wp-admin core files? Thanks in advance.
mike — Probably not. I’ve got a bit too much to do right now than to figure it out. Maybe sometime in the future.
Profashionelle — That’s much different than presetting content. You’d have to add in a meta box with a drop-down select and extra JavaScript. You’ll likely have to hire someone to build a plugin like that for you.
Just to jump in, sorry Justin, but took your idea and tied it in for a plugin I use on one of the sites I’m developing atm, qtranslate, its a multple lang plugin, anyway down to the nitty gritty this plugin handles the post content really well, but when it comes to the excerpts you have to add shortcode for each lang that the post will be in so for instance
[:en]english excerpt[:fr]french excerptso took your idea and twisted it for default excerpt content
function my_default_post_postexcerpt()
{
$dpc = "[:en]english excerpt[:fr]french excerpt";
return $dpc;
}
add_filter('default_excerpt', 'my_default_post_postexcerpt');
Thanks for the great problem solving again, I will post this over at the plugins forum with hat-off to you, thanks (the crazy loop through every image guy)
David — I’m happy to hear this tutorial will not die away in my blog archives. That’s what’s so great about the WordPress community — we can take various ideas and tie them together to make even better things.
@Profashionelle: Check out the Post Templates plugin. I’ve used it in the past to do what you described.
Hi,
I’m also very interested in the possibility to preset custom fields – in fact, I’m using the idea of “related posts” from Justin Tadlock for implementing threads in wordpress. Threads are implemented with custom fields with a fixed key value (e.g : ‘thread’).
The idea of a thread is different from a discussion through comments – like this very interesting one – for the discussion happens at the level of posts only. Since a blog is more “personal” than a forum, the thread here is more like a “conversation with oneself”.
So I needed to be able to preset the custom fields meta box, which is hardly possible, so I followed the advice or having an additional meta box that just enable to:
1) see existing threads if one wants to relate a new post to an existing thread,
2) enter a new thread title.
If any one is interested, I can discuss the code with you.
Thnks for this … thread
Catherine
@Jason Pelker
Thank you, Jason! I just downloaded the plugin and installed it on my blog and it works great so far. Definitely will be a great timesaver. Just wish there’d be a possibility to have a drop down template posts menu in the ADD NEW tab, but you just can’t have it all, I guess.
Profashionelle, have a look at the More Fields plugin (http://wordpress.org/extend/plugins/more-fields/). It will create a new page (or post) template, AND add it to the top-level Admin menu (like posts and pages). I’ve used it to create an “Events” menu item, with “Add new (event)” and “Edit (event)” as subitems.
Its very customisable, and works great. There are some others out there (Flutter, Pods, Supple Forms), but Ive found More Fields to have the most functionality for what i need.
Give it a try
Fantastic! Just what I needed and thankyou so much. For the benefit of the less proficient php coders such as myself could I just suggest folks that you have to omit the introductory “” in the supplied code.
It took me a couple of experiments to realise why I was getting an error……
Oops – lost the code – it was the
darn it [code] [/code]
Is there any way to include a couple of php expressions here? (Told you I wasn’t proficient……)
Hey, thanks for the code, I will just try this out.
Tys for the tip.
It fits like a glove.
Ow
i got a doubt about it.
How can i insert HTML(like a picute or something) code into the preset text?
Tys for the atention.
Hi guys! Just found this article, and is almost what I was looking for…
Like many of you, I would like to be able to get the preset text in a WYSIWYG editor made with the “More Fields” plugin, and not just in the main editor.
Anyone had any luck with that?
Thanks! Nice article about presets text in posts. Until now i use a plugin to insert text in Wordpress. I will try this script.
Is there any solution to automatically add shortcode for content wrapping in wordprss post box?
Like this:
[abc]
content content content
content content content
[/abc]
(Because my content are fetch from other source, so I want it auto)
If you know this, please help me.
My version of preseting text and custom fields. It is workable and tested in WP3
It’s works with only new posts
Hi,
This sounds like it is exactly what I need. Unfortunately when I tried it in the Wordpress editor, my screen went completely white.
Any ideas as to what I’ve done wrong? Does it matter where I insert the code into the file ?
Thanks
Hi,
This preset text idea is something similar. But I’m actually looking for a code to add menu in post/page editor where I can click on menu – pop ups small window – I insert Youtube video ID – then press ok – and line of code something like ‘ytaudio(code-I-input)’ paste in post.
Purpose of using is ‘yt-audio’ plugin with menu on post editor, o/w I’ve to type ‘ytaudio…’ each time I embed Youtube audio.
Thanks for help in advance.
Hum
@zach “I tried it in the WordPress editor, my screen went completely white. ”
I had the same issue. Use the code provided by Grof
Type in your content like so:
Don’t use the tag that screws up the code.
You, sir, are a legend. Thank you for this amazing tip!
Justin,
You’re a life saver. Thanks for taking on the challenge. This tiny feature made a huge difference!!!
Thanks again,
Jose
If you use custom post types (like I do) you can do this as well:
Thanks so much, Nicole! I really needed this for a site I’m working on! I just needed content to be auto-inserted for a specific post type only. Perfect…
This might be a weird addition, but I had to do this for a client’s site, so this might help someone else. If you want to populate an empty editor with the category description, here’s what you need to do:
`function preset_content() {
global $post;
$cat = get_the_category($post->ID);
if(empty($cat)) {
$default_content = “Please save the post’s category to show default content”;
} elseif ( $post->post_content == “Please save the post’s category to show default content” and $post->post_type == ‘post’ ) {
$default_content = esc_attr($cat[0]->description);
} else {
$default_content = $post->post_content;
}
return $default_content;
}
add_filter(‘the_editor_content’, ‘preset_content’);`
I know this post is 3 years old, but I just wanted to say, I really appreciate this code very much!
The code is so simple & still works perfect after all the WP updates over the last 3 years!
Great job!
How could I use this to pre-fill the Excerpt field?
Thanks.
Hello, I believed I understood how to use the feature put it leads to crash the whole website.
I’ve replaced the functions.php through the wordpress editor. Now nor the admin nor the website are accessible anymore. I obviously pasted the code in a wrong way.
Here is the error I get : Fatal error: Call to undefined function load_theme_textdomain() in /homez.504/ninjapre/www/wp-includes/functions.php on line 11.
Where line 11 is the first line that call a function, the previous are comments.
I’ve tried to replace the functions.php file by the original one, but I stil have the same error.
Have you ever heard about such an issue ? Do you have any clue how to solve it ?
Great article, thank you. However, is it possible to link this to a specific template. E.g. I’m creating a table template (template_tables.php) as my users won’t know how to add a table class. So I’d like the table to appear for them if they select this template. Your advice HUGELY appreciated. =)
Justin, this tip is the gift that keeps on giving! I now have default text inserted into my excerpt field (a shortcode which gives the publication date), just as I wanted. Almost – now I need to make it only appear for certain post types, but that can’t be too difficult…
The only surprise is that I did a search on t’web, instead of coming here directly
Gary
How tot do this with old NOT empty posts?
Any ideas?
thanks!