Include Post Format in permalink

While researching this topic myself, I found a plugin called Post Format Permalink. However, this plugin is not compatible with recent versions of WordPress; it is also filled with unnecessary code.

I forked the plugin’s repository on GitHub, and improved the code greatly. I can now use a %post_format% tag in my permalink structure, and that works fine. Mostly.

The problem is, posts with no post format are displaying as http://example.com/standard/just-another-post, which is not the desired outcome. I’ll keep working on this, and post an update here.

Here is the code I used. It can also be found on GitHub:

<?php
/**
 * Plugin Name: Post Format Permalink
 * Plugin URI: https://wordpress.stackexchange.com/q/70627/19726
 * Description: Include the post format slug in your permalinks. Simply use the <code>%post_format%</code> tag as part of your custom permalink.
 * Version: 1.2
 * Author: shea
 * Author URI: https://wordpress.stackexchange.com/users/19726
 */

add_filter( 'post_link', 'post_format_permalink', 10, 2 );
add_filter( 'post_type_link', 'post_format_permalink', 10, 2 );

function post_format_permalink( $permalink, $post_id ) {

    // if we're not using the %post_format% tag in our permalinks, bail early
    if ( strpos($permalink, '%post_format%') === FALSE ) return $permalink;

    // get the post object
    $post = get_post( $post_id );
    if ( ! $post ) return $permalink;

    // get post format slug
    $format = get_post_format( $post->ID );

    // set the slug for standard posts
    if ( empty( $format ) )
        $format = apply_filters( 'post_format_standard_slug', 'standard' );

    // apply the post format slug to the permalink
    return str_replace( '%post_format%', $format, $permalink );
}

error code: 523