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 working on a system that forces users (unfamiliar with wp) to choose a category before creating a new post, and then adds some appropriate html layout styling to help them.

The following code works if they choose category "x". How can it be adapted to cope with a couple of alternative category situations?

Human Code Required:

  • If the new post is category X: add this html.
  • If the new post is category Y: add this html instead.
  • If the new post is any other category: add this html.

Existing Code - Inserts some html to new posts if the category is "X":

// This function will only be called when creating an empty post,
// via `get_default_post_to_edit()`, called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
    wp_set_post_categories( $post_id, $_REQUEST['category_id'] );
}

add_filter( 'default_content', 'wpa70073_default_products_content' );

function wpa70073_default_products_content( $content ) {

    // change this to your desired category ID
    $products_category_id = 4;

    if( 
        isset( $_REQUEST['category_id'][0] )
        && $products_category_id == $_REQUEST['category_id'][0] 
        )
        return 'some html styling';
}

UPDATE: Tried adding this to the end, but it didn't work...

// change this to your desired category ID

$products_category_id = 4;................

    if( 
        isset( $_REQUEST['category_id'][0] )
        && $products_category_id == $_REQUEST['category_id'][0] 
        )
        return 'Some html styling';

    if ( $products_category_id = '6' ) {
        return 'Some alternative html styling';
    } else {
        return 'Some default html styling';
    }
}

Also tried editing the code along the lines of the cpt thread suggested below, but again no luck :(

add_filter( 'default_content', 'wpa70073_default_products_content' );
function wpa70073_default_products_content( $content ) {
    switch($content->category){
        // change this to your desired category ID
        case'4':
            $content='cat 4 content';
            break;
        case'6':
            $content='news content';
            break;
        default:
            $content='default content';
            break;
    }
    return $content;
}
share|improve this question

2 Answers 2

up vote 1 down vote accepted

You should have linked to the original Q&A since the beginning...
Another great code from @JanFabry, btw :)

Force category choice before creating new post?

Using exactly the code provided in his answer, this is how to make the filter default_content work:

add_filter( 'default_content', 'wpse_71772_default_content', 10, 2 );

function wpse_71772_default_content( $content, $post ) 
{
    if( !isset( $_REQUEST['category_id'] ) )
        return $content;

    switch( $_REQUEST['category_id'][0] ){
        // change this to your desired category ID
        case'1':
            $content='cat 4 content';
            break;
        case'3':
            $content='news content';
            break;
        default:
            $content='default content';
            break;
    }
    return $content;
}
share|improve this answer

Considering the nature of filters wouldn't it be better to 'filter' the output rather then writing this to the database?

With your approach changing the post two years from now after you've generated thousands of items in the various categories will be a major pain. But filtering the output would be as simple as changing a line or two of code.

Here's a link to a very similar question Default or Preset Content for Custom Post Types

share|improve this answer
    
Thanks Stephen - As is probably painfully obvious, I have no idea how to write my own code. I'm guessing that you're suggesting adding the content AFTER the post has been created? If so it won't work for me. I'm simply trying to add some default divs and styling depending on the category chosen which is reflected in the editor window to help & assist users not familiar with wp... Let me know if that's not what you meant! –  speedypancake Nov 6 '12 at 23:02
    
My comment is more directed to the choice of using default content and storing it in the database along with the post or using a filter to append/insert the content before the page is rendered. –  Stephen Nov 6 '12 at 23:06
    
Thanks for the link - that's actually where I started out, but had to avoid custom post types as a lot of my templates depend on normal post categories for sorting & displaying content & don't play nicely with cpt's.. but maybe there's a structure in that code I can follow.. thanks –  speedypancake Nov 6 '12 at 23:07
    
The system I'm working on presents a dropdown menu when a site owner selects add new post. The menu lists the catgories and will setup the new post with approporiate divs and styling depending on the catgory chosen which can be seen in the edit window. If there's a better alternative way of achieving that I'd love to know. –  speedypancake Nov 6 '12 at 23:15
    
That clarifies it, from your original post it looked as if you were adding a single line of text. And for that I thought filtering on the output would be better. You're on the right track. –  Stephen Nov 6 '12 at 23:42

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.