2

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

4

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

3
  • 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
    Commented Aug 16, 2011 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
    Commented Aug 17, 2011 at 8:43
  • this doesn't seem to work. I get '<b>Notice</b>: Trying to get property of non-object in <b>/Users/jynk/Projects/630_Wonkhe/web/wonkheweb2/wp-content/themes/wonkhe2-theme/lib/default_content.php</b> on line <b>25</b><br />' which is ' switch( $post->post_type ) {'
    – v3nt
    Commented Feb 5, 2019 at 11:02
0

Try something more like:

function my_editor_content( $content ) {

 global $post

 if (get_post_type($post) == 'sources'){
 //rest of your stuff
5
  • Unfortunately, that is still returning "default!"
    – Isendra
    Commented Aug 16, 2011 at 0:40
  • Do you have any posts "saved" under your custom post types?
    – Wyck
    Commented Aug 16, 2011 at 0:56
  • I deleted all of the posts, etc. It is still showing up with the "default!"
    – Isendra
    Commented Aug 16, 2011 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
    Commented Aug 16, 2011 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
    Commented Aug 16, 2011 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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