Default or Preset Content for Custom Post Types

I am trying to modify the code for default content to display based on post type, but so far I have been unsuccesful. The base code is:

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
    $content = "default content goes here....";
    return $content;
}

My modifications include:

add_filter( 'default_content', 'my_editor_content' );

function my_editor_content( $content ) {
    if ( 'sources' == get_post_type() ) {
        $content = "Please insert an image of the document into this area.  If there is no image, please descript the document in detail.";
        return $content;
    } elseif ( 'stories' == get_post_type() ) {
        $content = "Please write your reminiscences, recollections, memories, anecdotes, and remembrances in this area.";
        return $content;
    } elseif ( 'pictures' == get_post_type() ) {
    $content = "Please insert an image of a photograph into this area.";
    return $content;
    } else {
    $content = "default!";
    return $content;
};}

But this just isn’t working. I feel like I have missed the obvious.

2 Answers
2

Use the second parameter $post and check $post->post_type alongside a switch, it’s easier and nicer to work with than several if else if else, etc..

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

function my_editor_content( $content, $post ) {

    switch( $post->post_type ) {
        case 'sources':
            $content="your content";
        break;
        case 'stories':
            $content="your content";
        break;
        case 'pictures':
            $content="your content";
        break;
        default:
            $content="your default content";
        break;
    }

    return $content;
}

Hope that helps..

Leave a Comment