What triggers the WordPress register_uninstall_hook()?

I spent over an hour trying to figure out the user’s workflow when wanting to add an uninstall hook to a WordPress plugin. I did eventually stumble across the answer, which once you know probably seems blatantly obvious. However, it wasn’t for me at the time.

Question: How to trigger theĀ register_uninstall_hook() callback function?

Answer: Delete the plugin!

I spent a lot of my time looking for the uninstall link. Wondering if I was supposed to make my own. Not sure whether it being a Network Activated plugin made a difference. All the documentation and blog posts I could find just implied that it would all work out of the box. And, well, it does.

Delete Plugin And DataWhen you deactivate and then delete a plugin, it usually asks you for confirmation. With a plugin that has registered and uninstall hook, it adds some additional text. Personally I think that’s a little obfuscated, both to developers and to users. Maybe I’ll find the time to figure outĀ tracĀ andĀ submit a patch for recommending further emphasis. Hopefully, I will find somewhere to mention it on the wordpress.org documentation site.


Why does my WordPress SSL not work behind Nginx Proxy?

So, you’ve bought yourself an SSL certificate and set up Nginx to listen nicely on port 443. Your public facing load balancer/proxy/router is doing everything it should, pushing traffic upstream perfectly. But, dangit, WordPress just won’t play ball. That pesky is_ssl() function that gets used everywhere, always returns false.

Probably because you are proxying over http, not https. What you need to do is add an additional header in the configuration block so that you can look for it in WordPress.

It is actually quite well documented, if you can find it. The first two I found wereĀ WordPress Installation behind reverse-proxy ssl redirect loopĀ andĀ NGINX REVERSE PROXY AND WORDPRESS SSL ISSUES. Sorry about the shouting.

Maybe by posting this, I’ve maybe caught a few more frustrated key words, or raised the page rank of those I link to, helping some one else. I hope so!

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.

Creating Custom Boxes using Meta Box plugin With Filters instead of Global Variable

The Meta Box plugin is another go-to plugin for my WordPress projects. It allows me to quickly and easily define custom meta boxes for my custom post types.

However, I am not a fan of global variables, and the array used to define the metaboxes is not easy to traverse or inspect. Instead I prefer to use WordPress filter hooks, and passĀ around an array that is easily modified.

 * Use a filter to fetch a list of metaboxes
 * (so these can now be updated/removed/added to theme or other plugins)
 * Parse list into something the Meta Box plugin understands
 * Make Meta Box objects!

add_action( 'admin_init', 'tcb_register_meta_boxes' );
function tcb_register_meta_boxes(){
  if( !class_exists('RW_Meta_Box') )

  $_metaboxes = apply_filters( 'tcb_register_meta_boxes', array() );
  $metaboxes  = tcb_mb_parse_internal_list( $_metaboxes );

  foreach( $metaboxes as $metabox ):
    new RW_Meta_Box( $metabox );
 * Takes a list that can be more easily inspected (and filtered)
 *  converts into a format used by MetaBox plugin
function tcb_mb_parse_internal_list( $mb ){
  $metaboxes = array();

  foreach( $mb as $mbid => $mbdetails ):
    $fields = array();
    foreach( $mbdetails['fields'] as $mbfid => $mbfdetails ):
      $mbfdetails['id'] = $mbfid;
      $fields[]         = $mbfdetails;
    $mbdetails['id']     = $mbid;
    $mbdetails['fields'] = $fields;
    $metaboxes[]         = $mbdetails;

  return $metaboxes;
 * Example filter for my WP Glossary plugin
add_filter( 'tcb_register_meta_boxes', 'tcb_wpg_register_meta_boxes' );
function tcb_wpg_register_meta_boxes( $metaboxes ){
  $metaboxes['glossary_reference'] = array(
    'title'    => 'Glossary References',
    'pages'    => array('glossary'),
    'context'  => 'normal',
    'priority' => 'low',
    'fields'   => array(
      'wpg_ref_title' => array(
        'name'  => 'Reference Title',
        'desc'  => 'Give the reference a title',
        'type'  => 'text',
        'clone' => false,

  return $metaboxes;

I thought I’d let the code speak for itself. What do you think?

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.