54 responses to “How to preset text in the WordPress post editor”

  1. Peter

    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?

  2. Peter

    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.

  3. Gilbert

    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” ;-)

  4. Ray

    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.

    function my_default_post_content()
    {
    $dpc = "This is the default content.";
    return $dpc;
    }
    add_filter('default_content', 'my_default_post_content');
  5. Gilbert

    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.

  6. Rodjer

    Hey Justin, just found your website, really love it! Just want to say, keep it coming and i will follow you!

    Regards,

    Rodj

  7. yasemin

    I suppose that i edited a core page now i can see just a blank page :(

  8. Dan

    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/

  9. Weekend Links - Apr 11, 2009 | OMNINOGGIN

    [...] 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… [...]

  10. Ray

    @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”).

  11. Dan

    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.

  12. New Wordpress Plugin: Default Post Content | Apartment One Six

    [...] over at justintadlock.com made a post a few days ago about how to preset text in the WordPress post editor.  It’s a great post, with an interesting filter detailed.  In the comments, somebody [...]

  13. Peter

    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!

  14. Scrib

    Anyone know how to determine whether it’s a page or a post that’s being written?

  15. Scrib

    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]

  16. Scrib

    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 :P

  17. Profashionelle

    Hi Justin,

    thanks for this tutorial, definitely will try this out. I assume there is no way to have multiple presets?

  18. mike

    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.

  19. ChuckS

    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.

  20. Dan

    has anyone been able to do this with success?

  21. Peter

    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?

  22. mike

    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?

  23. Profashionelle

    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. :)

  24. David

    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 excerpt

    so 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)

  25. 101 Techniques for a Powerful CMS using WordPress | Noupe

    [...] are built-in actions and filter hooks that allow us to change things. Justin Tadlock is showing us 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. [...]

  26. Jason Pelker

    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.

    @Profashionelle: Check out the Post Templates plugin. I’ve used it in the past to do what you described.

  27. letondal

    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

  28. Profashionelle

    @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. :-)

  29. markgt

    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

  30. How to preset text in the WordPress post editor | WPThemes.com - WordPress Themes

    [...] you’re kind of a lazy person like I am, then here’s just the tutorial for you. Juston Tadlock has perfectly shown how to preset text in the WordPress editor. What does this mean? That you can [...]

  31. Bob Murray

    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……

  32. Bob Murray

    Oops – lost the code – it was the

  33. Bob Murray

    darn it [code] [/code]

  34. Bob Murray

    Is there any way to include a couple of php expressions here? (Told you I wasn’t proficient……)

  35. How to preset text in the WordPress post editor- Pro Blog Tutorials

    [...] WordPress forums, and found a link to who else, but the WordPress guru’s website, Justin Tadlock. Well, without a doubt, he had just the thing that is needed for preseting text in WordPress so [...]

  36. Robert Joe

    Hey, thanks for the code, I will just try this out.

  37. 10 Useful WordPress Hook Hacks « Tech7.Net

    [...] How to preset text in the WordPress post editor [...]

  38. 10 Useful WordPress Hook Hacks | X Design Blog

    [...] How to preset text in the WordPress post editor [...]

  39. 10 Useful WordPress Hook Hacks | huibit05.com

    [...] How to preset text in the WordPress post editor [...]

  40. Shopping Mall » Blog Archive » 10 Useful WordPress Hook Hacks

    [...] How to preset text in the WordPress post editor [...]

  41. 10 Useful WordPress Hook Hacks | Shabbir

    [...] How to preset text in the WordPress post editor [...]

  42. Ian mendonça

    Tys for the tip.

    It fits like a glove.

  43. Ian mendonça

    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.

  44. Bjarne Christensen

    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?

  45. Coafuri

    Thanks! Nice article about presets text in posts. Until now i use a plugin to insert text in Wordpress. I will try this script.

  46. Insert a text to text editor automatically! | WpFunc

    [...] All. Have a nice day. Thanks to Justin Tadlock [...]

  47. Jozy

    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.

  48. 10 tương tác Code WP hữu ích nhất : Code Vip

    [...] How to preset text in the WordPress post editor [...]

  49. 101 Techniques for a Powerful CMS using WordPress « Temas e Dicas para Wordpress

    [...] are built-in actions and filter hooks that allow us to change things. Justin Tadlock is showing us 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. [...]

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (e.g., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.