Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Pre-populate Post Type Content

DZone's Guide to

Pre-populate Post Type Content

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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;
}


CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

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 DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}