Over a million developers have joined DZone.

Pre-populate Post Type Content

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

With custom post types you can separate the content into different section, for example you can post types, pages, portfolio pages and products. If you find that you enter the same content for different post types each time then you can use the WordPress filter default_content to set a template of default content you can use as the default content for your post types.

Often people will ask for comments at the end of the post, such as:

Let me know your thoughts in the comments.

Instead of remembering to type this in each time you can setup the content editor to have this populated by default.

Or if you have a portfolio post type you can add some text at the bottom of each post to contact you about further work.

All you have to do is add the following code to your functions.php file.

add_filter( 'default_content', 'pu_default_editor_content' );

function pu_default_editor_content( $content ) {

    global $post_type;

    switch( $post_type ) 
    {
        case 'post':
            $content = 'Default content for blog posts.';
        break;

        case 'page':
            $content = 'Default content for pages.';
        break;

        case 'portfolio':
            $content = 'Default content for your portfolio pages.';
        break;

        case 'products':
            $content = 'Default content for blog posts.';
        break;
    }

    return $content;
}


Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}