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') )
    return;

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

  foreach( $metaboxes as $metabox ):
    new RW_Meta_Box( $metabox );
  endforeach;
}
/**
 * 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;
    endforeach;
    $mbdetails['id']     = $mbid;
    $mbdetails['fields'] = $fields;
    $metaboxes[]         = $mbdetails;
  endforeach;

  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?

Linode Doubles Available Disk Space

One of the most talked about and requested features on the Linode forums, is about disk space. Linode customers love the quality of service they get, but perceived value of disk space is lower than ever. Customers expect lots and lots of disk space. The applications they run are bigger and much hungrier for disk usage than they were a few years ago.

Linode Disk Space Doubled
Linode Disk Space Doubled

It had become an issue thorny enough that some have stated although they want to stay with Linode, they simply cannot justify the costs associated with storing that amount of data. Linode competitors might not have the same stability or long history of quality, but they did offer much more disk space for less money. And the bottom line matters in business.

Which makes Linode’s announcement all the more welcome. Storage space has been doubled. This could be just the healing salve on the worried brow of Linode customers on the cusp of leaving, having been enticed by cheaper disk space elsewhere.

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.