WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is a common function that adds default text to all posts:

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

How would this be changed to add the default content only to a post in one category?

4/10/11 Not an exact answer, but a few choices below in my own answer

share|improve this question
The only time that filter runs is for new posts, at that point in time there is no relationship between that post and any taxonomy. – t31os Apr 9 '11 at 16:48
Is it a solution to add an intermediate page that asks for the category when creating a new post? I did a similar thing recently, I can put it in an answer if you want. – Jan Fabry Apr 9 '11 at 21:00
I'd be interested in seeing in any case Jan, i'm curious like that... :) – t31os Apr 10 '11 at 9:51
@t31os: I created a new question and answer for this, because it can be useful on its own. If someone wants to go the Ajax route for this question I would be very interested too! – Jan Fabry Apr 10 '11 at 11:39
@t31os, That's interesting, thanks. Guess I'll need a different route. @jan, I'll look at your code. – markratledge Apr 10 '11 at 15:47
up vote 3 down vote accepted

One possibility is this question/answer here by Jan Fabry, which asks for the default content in the process of creating the new post: Force category choice before creating new post?

I ended up using a Quicktag as a way of easily getting the content into a post, and because the default content happened to be html, it works as good as it can for now. But in the future there turns out to be a way to get default content into a post when that post is saved in that category, that will be good.

Quicktags function for functions.php:

//Custom Quicktags Function

function my_quicktags() {
    get_bloginfo('template_directory').'/custom-quicktags.js', array('quicktags'));
add_action('admin_print_scripts', 'my_quicktags');

Sample Quicktags code for custom-quicktags.js, which goes in the theme folder:

edButtons[edButtons.length] =
new edButton('newbutton1'
,'html, like <div>'
,'and more </div>'
share|improve this answer

You can globalize $post and check if it has the category you want:

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
    global $post;
    $the_one_category_id= '12'; //the category you want this to work on id
    $args= array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'ids');
    $cats = wp_get_object_terms($post->ID, 'category',$args);
    if (in_array($the_one_category_id,$cats)){
        $content = "default content goes here....";
        return $content;
share|improve this answer
Looks like it should work fine, but I get a php error: wrong datatype for second argument, I guess meaning in in_array() – markratledge Apr 9 '11 at 15:45
I see why, try the updated code with the array casting – Bainternet Apr 9 '11 at 15:55
The post_category field isn't actually used last time i checked. – t31os Apr 9 '11 at 16:32
Correct, i guess i'm a bit outdated on that, any changed the function, Thanks – Bainternet Apr 10 '11 at 6:59

Your Answer


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.