How to prepopulate post types in WordPress with custom content

Once you start writing several articles per day and you find yourself repeating the content, you might end up copying and pasting same stuff over and over again. Or even worse, you might end up losing quite amount of time repeating yourself.

Of course, repeating the same content over and over again isn’t such a good idea, but there will be stuff you will simply have to copy for each and every post you write.

In this article, we’re about to show you a simple code which will save you time in that case.

For example, let’s say you have a custom post type where you exclusively publish images, entire galleries or screenshots. And maybe you always want to end up your article by saying: “Enjoy the images” or “Tell us which your favorite is” or something like that. Instead of typing the phrase over and over again, let’s see how you can make WordPress write it for you.

  1. Open functions.php file
  2. Copy and paste the following code:
  3. add_filter( 'default_content', 'pu_default_editor_content' );
    function pu_default_editor_content( $content ) {
    global $post_type;
    switch( $post_type )
    {
    
    case 'post':
    $content = 'You can write anything you want here';
    break;
    
    case 'page':
    $content = 'You can write anything you want here';
    break;
    
    case 'portfolio':
    $content = 'You can write anything you want here';
    break;
    
    case 'products':
    $content = 'You can write anything you want here';
    break;
    }
    
    return $content;
    }
  4. Modify the $content variable and write any text you want under those apostrophes
  5. Save changes
  6. Check any post type; for example, try to add a new post and you will see that your text is already there

If you have a custom post type, you can simply add another case with the name of that post type.

Similarly, you can automatically add some custom text at the end of your posts and pages.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *