Set default content for custom post types in the WordPress editor

Article directory [hide]

  • Set the default content for the default post type (post)
  • Set default content for custom article types (products)
  • Set default content for multiple custom text types

In the “WordPress TinyMCE Editor Enhancement Skills”, Advocate has already mentioned the method of setting the default article content for the WordPress TinyMCE editor. Today, I mainly add how to set the default content for custom article types. What is a custom article type?

wpdaxue.com-201303471

Set the default content for the default post type (post)

To recap, the default post type of WordPress is post, we can add the following code in functions.php, that’s it:

1
2
3
4
5
6
7
8
add_filter (  'default_content' ,  'my_editor_content'  ) ;
 
function my_editor_content (  $content  )  {
 
	$content  =  "Welcome to contribute to WordPress University" ;
 
	return  $content ; 
}

Set default content for custom article types (products)

If you have a custom article type as product, then you can use the following code, pay attention to the if statement in the third line:

1
2
3
4
5
6
7
8
9
add_filter (  'default_content' ,  'default_products_content'  ) ;
 
function default_products_content (  $content  )  {
 
    if (  $_GET [ 'post_type' ]  ==  'products'  )  $content  =  "Default text content" ;
 
    return  $content ;
 
}

Set default content for multiple custom text types

The above example is to set the default content for only one article type. If you have many custom article types, you can use the following code to be more concise:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
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 ; 
}

The above code sets the default content for several article types (sources, stories, pictures and default). The last “default:” means that in addition to the above 3, other article types use its default content.

Reference material: http://justintadlock.com/archives/2009/04/05/how-to-preset-text-in-the-wordpress-post-editor

Leave a Reply

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