4

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

5
  • 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
    Commented Apr 9, 2011 at 16:48
  • 1
    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
    Commented Apr 9, 2011 at 21:00
  • I'd be interested in seeing in any case Jan, i'm curious like that... :)
    – t31os
    Commented Apr 10, 2011 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
    Commented Apr 10, 2011 at 11:39
  • @t31os, That's interesting, thanks. Guess I'll need a different route. @jan, I'll look at your code. Commented Apr 10, 2011 at 15:47

2 Answers 2

3

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() {
    wp_enqueue_script('custom_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'
,'TagButtonName'
,'html, like <div>'
,'and more </div>'
,''
);
0

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;
    }
4
  • Looks like it should work fine, but I get a php error: wrong datatype for second argument, I guess meaning in in_array() Commented Apr 9, 2011 at 15:45
  • I see why, try the updated code with the array casting
    – Bainternet
    Commented Apr 9, 2011 at 15:55
  • 2
    The post_category field isn't actually used last time i checked.
    – t31os
    Commented Apr 9, 2011 at 16:32
  • Correct, i guess i'm a bit outdated on that, any changed the function, Thanks
    – Bainternet
    Commented Apr 10, 2011 at 6:59

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.