90 Responses

  1. Peter
    Peter Published |

    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?

    Reply
  2. Peter
    Peter Published |

    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.

    Reply
  3. Gilbert
    Gilbert Published |

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

    Reply
  4. Ray
    Ray Published |

    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');
    Reply
    1. Tosho Hendlin
      Tosho Hendlin Published |

      Hey Ray, I’d like to submit this as a plugin for the Wordpress community. Is that alright with you?

      Reply
  5. Gilbert
    Gilbert Published |

    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.

    Reply
  6. Rodjer
    Rodjer Published |

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

    Regards,

    Rodj

    Reply
  7. yasemin
    yasemin Published |

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

    Reply
  8. Dan
    Dan Published |

    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/

    Reply
  9. Weekend Links - Apr 11, 2009 | OMNINOGGIN
  10. Ray
    Ray Published |

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

    Reply
  11. Dan
    Dan Published |

    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.

    Reply
  12. New Wordpress Plugin: Default Post Content | Apartment One Six
  13. Peter
    Peter Published |

    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!

    Reply
  14. Scrib
    Scrib Published |

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

    Reply
  15. Scrib
    Scrib Published |

    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]

    Reply
  16. Scrib
    Scrib Published |

    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

    Reply
  17. Profashionelle
    Profashionelle Published |

    Hi Justin,

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

    Reply
  18. mike
    mike Published |

    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.

    Reply
  19. ChuckS
    ChuckS Published |

    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.

    Reply
  20. Dan
    Dan Published |

    has anyone been able to do this with success?

    Reply
  21. Peter
    Peter Published |

    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?

    Reply
  22. mike
    mike Published |

    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?

    Reply
  23. Profashionelle
    Profashionelle Published |

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

    Reply
  24. David
    David Published |

    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)

    Reply
  25. 101 Techniques for a Powerful CMS using WordPress | Noupe
  26. Jason Pelker
    Jason Pelker Published |

    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.

    Reply
  27. letondal
    letondal Published |

    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

    Reply
  28. Profashionelle
    Profashionelle Published |

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

    Reply
  29. markgt
    markgt Published |

    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

    Reply
  30. How to preset text in the WordPress post editor | WPThemes.com - WordPress Themes
  31. Bob Murray
    Bob Murray Published |

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

    Reply
  32. Bob Murray
    Bob Murray Published |

    Oops – lost the code – it was the

    Reply
  33. Bob Murray
    Bob Murray Published |

    darn it [code] [/code]

    Reply
  34. Bob Murray
    Bob Murray Published |

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

    Reply
  35. How to preset text in the WordPress post editor- Pro Blog Tutorials
  36. Robert Joe
    Robert Joe Published |

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

    Reply
  37. 10 Useful WordPress Hook Hacks « Tech7.Net
  38. 10 Useful WordPress Hook Hacks | X Design Blog
  39. 10 Useful WordPress Hook Hacks | huibit05.com
  40. Shopping Mall » Blog Archive » 10 Useful WordPress Hook Hacks
  41. 10 Useful WordPress Hook Hacks | Shabbir
    10 Useful WordPress Hook Hacks | Shabbir at |
  42. Ian mendonça
    Ian mendonça Published |

    Tys for the tip.

    It fits like a glove.

    Reply
  43. Ian mendonça
    Ian mendonça Published |

    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.

    Reply
  44. Bjarne Christensen
    Bjarne Christensen Published |

    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?

    Reply
  45. Coafuri
    Coafuri Published |

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

    Reply
  46. Insert a text to text editor automatically! | WpFunc
  47. Jozy
    Jozy Published |

    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.

    Reply
  48. 10 tương tác Code WP hữu ích nhất : Code Vip
  49. 101 Techniques for a Powerful CMS using WordPress « Temas e Dicas para Wordpress
  50. 101 Techniques for a Powerful CMS using WordPress « WPhub.biz
  51. Grof
    Grof Published |

    My version of preseting text and custom fields. It is workable and tested in WP3

    It’s works with only new posts

    function preset_content() {
    global $post;
    
    if ( $post->post_content == '' and $post->post_type == 'post' ) {
    $default_content = 'my preset content text';
    add_post_meta($post->ID, 'my custom field', 'my value', true);
    } else 
    {$default_content = $post->post_content;} 
    
    return $default_content;
    }
    
    add_filter('the_editor_content', 'preset_content');
    Reply
  52. zach
    zach Published |

    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

    Reply
  53. Hum
    Hum Published |

    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

    Reply
  54. How to preset text in the WordPress post editor | Equal Web Creative
  55. Chantal
    Chantal Published |

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

    $default_content = ' Title of the first paragraph
    	write your first paragraph here

    Don’t use the tag that screws up the code.

    Reply
  56. Killer hacks to enhance WordPress editor - CatsWhoCode.com
  57. Shun the Plugin: 100 WordPress Code Snippets from Across the Net | WordPress, Multisite and BuddyPress plugins, themes, news and help – WPMU.org
  58. Riaan
    Riaan Published |

    You, sir, are a legend. Thank you for this amazing tip!

    Reply
  59. Jose
    Jose Published |

    Justin,

    You’re a life saver. Thanks for taking on the challenge. This tiny feature made a huge difference!!!

    Thanks again,
    Jose

    Reply
  60. Nicole
    Nicole Published |

    If you use custom post types (like I do) you can do this as well:

    add_filter( 'default_content', 'my_editor_content' );
    
    function my_editor_content( $content ) {
    
        global $post_type;
    
        switch( $post_type ) {
            case 'sources':
                $content = 'your content';
            break;
            case 'stories':
                $content = 'your content';
            break;
            case 'pictures':
                $content = 'your content';
            break;
            default:
                $content = 'your default content';
            break;
        }
    
        return $content;
    }
    Reply
    1. Adam
      Adam Published |

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

      Reply
  61. 10 Useful WordPress Hook Hacks - Smashing Magazine | Smashing Magazine
  62. Nick Budden
    Nick Budden Published |

    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’);`

    Reply
  63. Jim
    Jim Published |

    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!

    Reply
  64. Chris
    Chris Published |

    How could I use this to pre-fill the Excerpt field?

    Thanks.

    Reply
  65. Nicolas
    Nicolas Published |

    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 ?

    Reply
  66. Karen
    Karen Published |

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

    Reply
  67. 100 WordPress Code Snippets from Across the Net « Tech Snippets
  68. 为WordPress编辑器设置默认内容 | 乌托『不再』邦
  69. Gary Taylor
    Gary Taylor Published |

    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

    Reply
  70. Martin
    Martin Published |

    How tot do this with old NOT empty posts?
    Any ideas?
    thanks!

    Reply
  71. Add something under rich text editor in WordPress post edit page | Jason Fang's Simple Site
  72. Wordpress tricks and shortcut functions | Dzine TipsDzine Tips
  73. [WordPress]为WordPress编辑器设置默认内容 | 在路上…
  74. Añadir texto predefinido en los posts nuevos | Ayuda WordPress | Creatuweb
  75. Muhammad Faheem Usama
    Muhammad Faheem Usama Published |

    what to do if we want to add some image with link (or can say HTML) as preset for editor???

    Reply
    1. Muhammad Faheem Usama
      Muhammad Faheem Usama Published |

      I have got the solution. If you want to add any html then use . So the code will be as given below:

      add_filter( 'default_content', 'my_editor_content' );
      
      function my_editor_content( $content ) {
      
      	$content = 'You Preset content include HTML Here';
      
      	return $content;
      }
      Reply
      1. GraphiX
        GraphiX Published |

        But if your html contains “” or ” it will just break the functions.php and show a white page. i would really like to know how you’ve wrote that code into the box without it looking like a garbled mess. Any chance of help with what i want to do is at the bottom of this page, you can contact me via my website by clicking on my name.

  76. آموزش هک های وردپرس + ۱۰ هک کاربردی |
  77. Добавляем дефолтный контент в ваш редактор записей WordPress | WPinCode — WordPress без плагинов
  78. Post by email test | Aakash Chakravarthy WP.com blog
  79. GraphiX
    GraphiX Published |

    Hello, i was hoping you could help me? If you see my feature website above. You’ll see i have a HTML block with each and every post a picture, Software Title name, Homepage, Download link; License, cost, contact etc all neatly in place.

    How can i achive this using HTML markup on multiple lines?

    For example here’s what i want but when i re-fresh it’s a white page.

    add_filter('default_content', 'my_editor_content');
    
    function my_editor_content( $content ) {
    
    $content = "";
    $content = "
    <p here>";
    $content = "
    <p here>";
    $content = "
    <p here>";
    $content = "
    <p here>";
    $content = "
    <p here>";
    $content = "
    <p here>";
    $content = "";
    
    return $content;
    }

    I’ve tried all on a single line, i just get a white page. I’ve tried multiple lines like above same thing.

    I’ve even tried following the same idea as this

    wprecipescom/how-to-automatically-insert-content-after-each-post

    Any help would be appreciated, as copying this in 50-60 times a day. every day is getting tiring.

    Reply
    1. GraphiX
      GraphiX Published |

      Wow that looks completely screwed up formatting it didn’t look like that when i posted it.

      Reply

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 (i.e., 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.

css.php