Inserting Default Content into the WordPress Visual Editor

I’ve been working on a WordPress layout which needs two columns of user editable content per page. There are many ways you could do this with WordPress, however to make it as easy as possible for the content editors I though it would be easier if the visual editor had some default layout information inserted when creating a post.

I knew there must be an easy way to do this using the action API in WordPress and I came across this article by WPRecipies which suggests putting the following code in the functions.php of your theme:

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "If you enjoyed this post, make sure to subscribe to my rss feed.";
return $content;
}

I inserted a couple of divs for the columns into mine so I also wanted to add some basic styling to the editor. I know this probably isn’t the best way and I’m sure someone will correct me, but I just added the style to the end of the TinyMCE stylesheet which can be found here in /wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css

EDIT: you can do this for custom post types too, just change the “myposttype” to be set to your type:

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content) {
    global $post_type;
    if($post_type=='myposttype'){
        $content .= "If you enjoyed this post, make sure to subscribe to my rss feed.";
    }
    return $content;
}

4 thoughts on “Inserting Default Content into the WordPress Visual Editor

  1. Hi Paul –
    Hi — this is perfect. But need one tweak. How can this be applied to one type of post only or even to a certain post?
    I’m not well-versed in php but tried the following:

    add_filter( ‘default_content’, ‘my_editor_content’ );

    function my_editor_content( $content ) {
    if ( is_singular( ‘mycustompost’ )) {
    $content = “This is some custom content I’m adding to the post editor because I hate re-typing it.”;
    return $content;
    }
    }

    I don’t get an error. It simply doesn’t work. Is there a way to apply such a conditional to the function? Or, does a function do its thing with no respect to conditions?

    Thanks — Mike

  2. Hi Mike,

    The is_singular function is used when the content is being displayed on the front-end of WordPress, whereas the function above adds default content into the post editor in wp-admin.

    If you want to conditionally add the same text to the bottom of a post you’ll probably have to edit your theme. I believe the default theme has a file called loop-single.php which you could probably just add the info too after the function the_content();

    Paul

  3. Hi Paul, thanks for the reply. I didn’t explain very well. I have a custom post type called “Recipes” in which I’ve a number of custom fields and a few taxonomy check boxes for user entry (cook time, directions, etc.). I want to keep the user from entering information into the usual text area, so would like to put a “Enter no information here. Please use the entry sections at the bottom” message IN the text box.

    To keep MY message from showing on the front end, I’ll have to remove the_content from the loop, I think. And I only want this set up on the one custom post type. I’ve racked my php-starved brain and can’t figure it out.

    Thanks, Mike

  4. Ah ok, that makes sense.

    I think you may want to use something like:

    add_filter( 'default_content', 'my_editor_content' );
    function my_editor_content( $content) {
        global $post_type;
        if($post_type=='myposttype'){
            $content .= "If you enjoyed this post, make sure to subscribe to my rss feed.";
        }
        return $content;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">