Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add some default content to my posts using this code in my functions.php:

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "My html content.";
return $content;
}

It works fine, but I would like to add the content to some categories only. This is tricky because the default content gets added while creating a new post, (so it is without a category). I followed this thread: Force category choice before creating new post? and the author came up with a way of forcing you to choose a category before wp creates the new post, but I don't know how to edit the default content code so it is only applies to some categories?

share|improve this question

2 Answers 2

default_content runs when a post is loaded into the editor, actually. You can check for categories when the post is saved, but you'd want the save_post hook, probably. You want to check the $_REQUEST global.

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
  global $_REQUEST;
  if (isset($_REQUEST['post_category']) && in_array($some_category_id,$_REQUEST['post_category'])) {
    $content = "My html content.";
  }
  return $content;
}

... assuming no syntax errors. :)

You probably also want to check if the post_content is empty and insert only it is.

share|improve this answer
    
Thank you for your suggestion. Unfortunately I'm too dumb to make it work. Should I be replacing "some_category_id" with the category I want to have the default content applied to, or replace "post_category"??? I did try both, but I just get a blank space with no admin bar etc. Am I correct in thinking this would replace the original code I was using? Many thanks again - Excited we might get this working :) –  speedypancake Oct 16 '12 at 17:11
    
Yes, replace $some_category_id with an actual category ID. Edited my code. Try the new version. –  s_ha_dum Oct 16 '12 at 17:21
    
Thanks again - I edited your code like this but it still returns a blank screen - so frustrated 'cause I feel we are almost there.... add_filter( 'default_content', 'my_editor_content' ); global $_REQUEST; var_dump($_REQUEST); // show you what you have to work with if (isset($_REQUEST['post_category'])) && in_array($product,$_REQUEST['post_category'])) { function my_editor_content( $content ) { $content = "My html content."; } return $content; } –  speedypancake Oct 16 '12 at 21:14
    
What you pasted into the previous comment does not match my edited code. I originally pasted something really stupid. Start over with the new code. –  s_ha_dum Oct 16 '12 at 21:36
    
Good grief. I've never been here before & I don't know where to find your "new code". Looking at these posts, all I can see is the stuff I tried / posted. I assumed you had edited your original code. If not, where is the updated version???? :O –  speedypancake Oct 16 '12 at 22:03

You can use the_content filter to add content to a category.

function my_category_content ($content) {
         global $post;
         if(is_single()) {
           if (in_category("My Category", $post->ID)) {
             $content .= 'Add something';
           }
         return $content;
         } 
} 
add_filter('the_content', 'my_category_content');

This example will only display the added content on single post pages though.

share|improve this answer

Your Answer

 
discard

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.