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.

Update / Question clarification: How could you modify the following code from this thread: Force category choice before creating new post? which offers a drop-down menu to choose a category BEFORE creating a new post to add some default html content to a specific category???

add_filter( 'load-post-new.php', 'wpse14403_load_post_new' );
function wpse14403_load_post_new()
{
$post_type = 'post';
if ( isset( $_REQUEST['post_type'] ) ) {
    $post_type = $_REQUEST['post_type'];
}
// Only do this for posts
if ( 'post' != $post_type ) {
    return;
}
if ( array_key_exists( 'category_id', $_REQUEST ) ) {
    add_action( 'wp_insert_post', 'wpse14403_wp_insert_post' );
    return;
}
// Show intermediate screen
extract( $GLOBALS );
$post_type_object = get_post_type_object( $post_type );
$title = $post_type_object->labels->add_new_item;
include( ABSPATH . 'wp-admin/admin-header.php' );
$dropdown = wp_dropdown_categories( array(
    'name' => 'category_id[]',
    'hide_empty' => false,
    'echo' => false,
) );
$category_label = __( 'Category:' );
$continue_label = __( 'Continue' );
echo <<<HTML
<div class="wrap">
<h2>{$title}</h2>
<form method="get">
    <table class="form-table">
        <tbody>
            <tr valign="top">
                <th scope="row">{$category_label}</th>
                <td>{$dropdown}</td>
            </tr>
            <tr>
                   <td></td>
                   <th><input name="continue" type="submit" class="button- primary"            value="{$continue_label}" /></th>
           </tbody>
        </table>
       <input type="hidden" name="post_type" value="{$post_type}" />
   </form>
</div>
     HTML;
    include( ABSPATH . 'wp-admin/admin-footer.php' );
   exit();
}
// 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'] );
}

Original Q: I'm working on an ecommerce site & I'd like to automatically add some default content for posts in a specific category eg. products.

I have found a way to add some default content to all posts, but am unable to restrict it to just one category.

I've ended up using custom post types in the meantime, but it's a hassle persuading widgets like recent posts and post sliders to play with them.

Any ideas most welcome - Thanks

share|improve this question
    
What you mean by default content? The content to show on the site if the published post has no content? –  Mridul Aggarwal Oct 22 '12 at 11:39
    
The default content is some html with divs which split the post into areas for images and a product description. –  speedypancake Oct 22 '12 at 11:43
    
I mean do you want to show some content on the front-end if post content is blank or do you want to show some content in the content editor in back-end when creating new posts? –  Mridul Aggarwal Oct 22 '12 at 11:49
    
It's for the content editor. Just trying to make it easy for non wp users to list products. The default content splits the post into sections for images and description etc Thanks –  speedypancake Oct 22 '12 at 11:53
    
Ideally I could tweak something like this so it only applies to a specific post category... "add_filter( 'default_content', 'my_editor_content' ); function my_editor_content( $content ) { $content = "If you enjoyed this post, make sure to subscribe to my rss feed."; return $content; }" –  speedypancake Oct 22 '12 at 12:01

2 Answers 2

up vote 0 down vote accepted

Using the code you posted, the selected category's ID is available in the $_REQUEST, you can check that ID against your products category in the default_content filter and add content if there's a match:

add_filter( 'default_content', 'wpa70073_default_products_content' );
function wpa70073_default_products_content( $content ) {

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

    if( isset( $_REQUEST['category_id'][0] )
    && $products_category_id == $_REQUEST['category_id'][0] )
        return "<div>some default product content</div>";
}
share|improve this answer
    
Aw Milo - Thanks for putting up with me, and more importantly for answering my question :) Can't try it right now, but will later this evening - fingers crossed etc. Cheers –  speedypancake Oct 23 '12 at 17:40
    
Absolutely Brilliant! And they said it couldn't be done :) Thanks a ton Milo –  speedypancake Oct 23 '12 at 18:15
    
Just a quick follow-up. By adding some simple default divs etc to specific categories, I've been able to create easy to use product templates to help users (who are not experienced with wordpress) list products with eshop. I did have a version before that used custom post types, but most of my themes use normal post categories to sort and display content. Now I have a wide choice of content sliders, plugins and widgets to create some great looking ecommerce themes. NOTE: I found I had to add single 'batwings' to enclose my default content - Thanks again Milo - You're a star :) –  speedypancake Oct 23 '12 at 19:16

Showing some default content in the Content editor based on category is NOT possible. Let me explain

When you click the add new button, wordpress creates a default object in memory to populate the form it shows you, this object has an id of 0. Now when you hit the save button wordpress takes the data you entered & creates the record in database. So for a new post, the post doesn't exist in database & thus it's not linked to any category at all. This makes it impossible to differentiate between the posts of the category & not of the category.

The best you can do is to add a javascript on the page which listens to the categories onchange event & then change the text of that field but again it's not possible for the content since TinyMCE works inside an iFrame & javascript doesn't have access to it

share|improve this answer
    
Yeah, I believe you're right. I did see a similar post where they set it up so you had to choose a category before a post was created. That bit worked OK, but the default content never got added. wordpress.stackexchange.com/questions/14361/… Guess I'll have to stick with the custom post types for now - Thanks for your help :) –  speedypancake Oct 22 '12 at 12:11

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.