Add Default Text for WordPress Pages and Posts

Categories: Blog, Development, WordPress
Tags: , , , ,

Nov 5

I’ve seen countless posts about adding default text to new pages/posts but never thought it was that practical.  Mainly because the code that was presented would always add the same default text to both new pages and new posts in WordPress.

While it wasn’t a huge deal I often avoided using it for client work because of all the questions that would come up. If they wanted the default text in new posts, they’d question why it was also coming up for new pages as well.

I thought it was time to dive into this code myself and see if there was a simple way to add this default content according to the post type. There was, and it’s an easy solution. Copy the code below to your theme’s functions.php file, edit the default text for the page and post and save the file.

The Code

function dmg_default_text_in_editor( $content, $post ) {
  if ( 'page' == $post->post_type ) $content .= 'Default page content.';
  if ( 'post' == $post->post_type ) $content .= 'Default post content.';
  return $content;

add_filter( 'default_content', 'dmg_default_text_in_editor', 10, 2 );

What’s Going On

By using the WordPress add_filter() function we’re working with the default_content hook to call our function and make changes to the content by returning the default text we want to use when a new page or post is created.

If you look at the function that we’ve defined we’re passing two arguments – the content and the post data. The $post argument is an array which contains, among other post details, the post type – which we can check to assign default text to both new pages or posts (or pages and posts).

You’ll notice in the example that I append my default text to $content before returning it. Typically at the creation of a new page or post the content is going to be empty – so why bother? Simply put, I never know what plugins or functions are going to be added at a later date.  At least this way, if there were another function added that assigned default text, both would run and both default texts would show up*.

* I know that this would depend on a number of factors though, but better to play it safe.

One Last Note

There are additional default_xxx hooks you can play with too if you’d like. For example default_excerpt and default_title – you might not be using these all the time but there are some great uses at times.



Leave a comment

  • Jan 24, 2013
    @ 4:10AM

    sleno says:

    here is another solution – inserting the following codes into functions.php:

    function add_before_content($content) {
    if ( ‘page’ == $post->post_type ) return $content .’Default page content.’;
    if ( ‘post’ == $post->post_type ) return $content .’Default post content.’;
    add_filter(‘the_content’, add_before_content);


    function add_before_content($content) {
    return ‘Default Message’.$content;
    add_filter(‘the_content’, add_before_content);

  • Leave a comment

    Read comments


    Please do not submit your comment more than once. It will appear once it has been approved.