Auto-Create Excerpts for your WordPress Custom Post Types

With most website projects, you end up having content. That content has details and is often grouped in lists. When you are listing your items you want a bit more than their title, but not the whole description showing. And so excerpts are great.

They are, however, a bit of a resource hog. If there isn’t an excerpt, then WordPress creates one as it goes. It doesn’t save it to the database. This is an expensive operation. Stripping out the mark up, cutting it down to so many words or characters. And this is nearly always done in the context of a list, so there are lots of instances of this happening. On every page load. That can have a significant impact on your site’s performance.

It seems to me that their is a reasonable call for an auto-excerpt. By default, the user is not interested in setting the excerpt. The system (web site) should create the excerpt when the content is updated. It happens once, in the admin, and is just as transparent as if it were being generated on the fly as WordPress core does.

There are four steps to achieving this in an easily re-usable way. I’m going to continue to use my extras array that I made use of in my uber change enter title here post.

Step 1: Re-create the excerpt filter function from core

I don’t like it that WordPress core adds the more/continue link in the actual excerpt. I feel that’s more of a design issue than a content one. It is easy enough to build your own, if you want it, and then it can change depending on context. So I left that bit off.

Step 2: A short function to set the excerpt

It is important to disable the filter when updating, or you’ll get stuck in an infinite loop.

Step 3: Hook into save_post filter to determine when to write the auto-excerpt.

Step 4: Add it to you custom post type definition

Additionally, if you were looking closely, you can see that the auto-excerpt behaviour changes if you add excerpt support to the post type, allowing the user to dictate the excerpt content.

Get your WordPress Posts 2 Posts Related Items in the Same Order As They Are Sorted In Admin

I use the Posts 2 Posts plugin in nearly all my projects. It is one of my go-to plugins. When building relationships, it is possible to allow the related items to be sortable. What I then like to do is retrieve those related posts in that same order. Here’s an example of how to do that.

This snippet assumes that there are two custom post types (albums and tracks), and that there is a p2p relationship named album_tracks.

// $album_id is the $post->ID for the 'albums' post
function tcb_get_album_tracks( $album_id ){
  $args = array(
    'connected_type'    => 'album_tracks',
    'connected_items'   => $album_id,
    'suppress_filters'  => false,
    'nopaging'          => true,
    'connected_orderby' => '_order_from',
    'connected_order'   => 'asc'
  );
  $tracks = get_posts( $args );
  return $tracks;
}

I usually explicitly pass around post IDs rather that rely on the global post variable all the time, so your way may vary a little.

How To Change ‘Enter title here’ Text for any WordPress Custom Post Type (Über Way)

I seem to always be adding filters for my Custom Post Types to change the ‘Enter title here’ text. It feels like a lot of copy-pasta, and I wondered if there was a better way. Here is my little incremental improvement.

I’ve come up with a function that allows me to inspect the custom post type object and update the text. A single filter for all the post types.

So here is the the original WP Glossary post type definition:

$labels = array(
  'name'               => __( 'Glossary Terms',                   WPG_TEXTDOMAIN ),
  'singular_name'      => __( 'Glossary Term',                    WPG_TEXTDOMAIN ),
  'add_new'            => __( 'Add New Term',                     WPG_TEXTDOMAIN ),
  'add_new_item'       => __( 'Add New Glossary Term',            WPG_TEXTDOMAIN ),
  'edit_item'          => __( 'Edit Glossary Term',               WPG_TEXTDOMAIN ),
  'new_item'           => __( 'Add New Glossary Term',            WPG_TEXTDOMAIN ),
  'view_item'          => __( 'View Glossary Term',               WPG_TEXTDOMAIN ),
  'search_items'       => __( 'Search Glossary Terms',            WPG_TEXTDOMAIN ),
  'not_found'          => __( 'No Glossary Terms found',          WPG_TEXTDOMAIN ),
  'not_found_in_trash' => __( 'No Glossary Terms found in trash', WPG_TEXTDOMAIN )
);

register_post_type( 'glossary', array(
  'public'               => true,
  'menu_position'        => 105,
  'has_archive'          => true,
  'supports'             => array( 'title', 'editor', 'thumbnail', 'author', 'excerpt' ),
  'rewrite'              => array('slug' => __('glossary', WPG_TEXTDOMAIN)),
  'labels'               => $labels,
);

And this is how to trigger the filter.

...
register_post_type( 'glossary', array(
  'public'               => true,
  'menu_position'        => 105,
  'has_archive'          => true,
  'supports'             => array( 'title', 'editor', 'thumbnail', 'author', 'excerpt' ),
  'rewrite'              => array( 'slug' => __( 'glossary', WPG_TEXTDOMAIN ) ),
  'labels'               => $labels,
  'extras'               => array( 'enter_title_here' => 'WPG Glossary Name' )
);

You can add just the $extra bit to the register_post_type function for any post type and the filter will pick it up.

Change “Enter Title Here” text for your WordPress custom post type

When you add a new post, WordPress pre-fills the title input box with the gentle greyed out text ‘Enter title here’. It does this by default for all post types, including any custom ones you have registered. This might not always make sense.

As of WordPress 3.1 there is now a filter to easily change this.

add_filter( 'enter_title_here', 'tcb_enter_title_here' );
function tcb_enter_title_here( $message ){
  global $post;
  if( 'album' == $post->post_type ):
    $message = 'Enter Album Name';
  endif;

  return $message;
}

The above snippet will replace the text for the post type ‘album’.

UPDATE

I have since found an even better way of handling this.

Adding custom styling to WordPress custom post types admin screens

My WordPress projects usually end up using at least 3 custom post types, sometimes more than 10. Together with some extra admin screens (from external plugins[1]), the admin menu gets cluttered. All the various edit post admin screens can look very similar to the untrained eye.

What to do about!? Well, I’m not a designer, but I can certainly kick-start things by providing inspiration to my colleagues. What I need is some way of boldly showing them that we can add some styling to specific post type admin screens.

add_filter( 'admin_body_class', 'tcb_custom_post_type_body_class' );
function tcb_custom_post_type_body_class( $classes ){
  global $post;

  $post_class = get_post_type( $post->ID ) . '_admin';
  if( is_array($classes) ):
    $classes[] = $post_class;
  else :
    $classes .= " {$post_class}";
  endif;

  return $classes;
}

That will add a class to the page’s body tag, which can be targeted with CSS. Here is a rather pathetic example for my albums and artists post types:

.albums_admin{
  background-color: #0f0;
}
.artists_admin{
  background-color: #f00;
}

Good luck with that!