1

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

7
  • What you mean by default content? The content to show on the site if the published post has no content? Commented Oct 22, 2012 at 11:39
  • The default content is some html with divs which split the post into areas for images and a product description. Commented Oct 22, 2012 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? Commented Oct 22, 2012 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 Commented Oct 22, 2012 at 11:53
  • 1
    Possible duplicate of Adding default post content for category
    – T.Todua
    Commented May 16, 2016 at 6:27

2 Answers 2

1

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>";
}
3
  • 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 Commented Oct 23, 2012 at 17:40
  • Absolutely Brilliant! And they said it couldn't be done :) Thanks a ton Milo Commented Oct 23, 2012 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 :) Commented Oct 23, 2012 at 19:16
1

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

1
  • 1
    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 :) Commented Oct 22, 2012 at 12:11

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.