Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
You sir, are the winner. That worked flawlessly. I'm not sure why a switch would work where an if statement wouldn't, but it doesn't matter. This is extremely useful, you should consider posting it to the "Best Collection of Code for your functions.php file" thread. –  Isendra Aug 16 '11 at 22:09
    
A switch is just more convenient for variables that have a limited set of possible values that you know, and it's easier to manage(plus it looks more readable) than numerous if/else statements. Happy to help.. ;) –  t31os Aug 17 '11 at 8:43

Try something more like:

function my_editor_content( $content ) {

 global $post

 if (get_post_type($post) == 'sources'){
 //rest of your stuff
share|improve this answer
    
Unfortunately, that is still returning "default!" –  Isendra Aug 16 '11 at 0:40
    
Do you have any posts "saved" under your custom post types? –  Wyck Aug 16 '11 at 0:56
    
I deleted all of the posts, etc. It is still showing up with the "default!" –  Isendra Aug 16 '11 at 2:06
    
No I meant have you created and saved a post for type "sources" and then navigated to it, theoretically it should work, if not maybe echo out get_post_type() in the actual template and see if it is working. –  Wyck Aug 16 '11 at 4:04
    
Yes, I have a custom post type for sources. I know it's the right post type because the admin editor url is "/wp-admin/post-new.php?post_type=sources" and the published page is pulling the single-sources.php template. I just tried publishing a page and it published as "default!" not the correct preset. It is also showing up as "default!" in the main content block in the post panel. –  Isendra Aug 16 '11 at 4:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.