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

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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


Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

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