Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm running the latest version of WordPress with pretty permalinks enabled.

I'm looking for a way to include the Post Format type (eg: link, status, quote) in the post's permalink. When the post has no format assigned, or uses the 'standard' format, I want that part of the permalink to empty.

Example using the link post format:

http://example.com/link/look-at-this-cool-site/

Example using the quote post format:

http://example.com/quote/example-quote-post

Example using no post format:

http://example.com/just-another-post

Edit: See my answer for progress

share|improve this question
    
do child attachment post URLs still need to function? for example /link/look-at-this-cool-site/attachment-post/ or /just-another-post/attachment-post/ –  Milo Nov 1 '12 at 6:36
    
@Milo Yes, they would. Just as if you were using %category% as part of your permalink structure. –  bungeshea Nov 1 '12 at 9:37
    
the one thing that makes it vastly more complicated is your requirement to have no format for posts without a format, that's not how %category% permalinks work, so it's not quite the same. without a format, your posts would clash with pages, or you'll need extra rewrite rules and some fairly complex parsing to make attachment posts work. –  Milo Nov 1 '12 at 15:01
add comment

2 Answers

up vote 2 down vote accepted

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://github.com/bungeshea/post-format-permalink
 * 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 Bunge
 * Author URI: http://bungeshea.com
 */

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 );
}
share|improve this answer
    
Great solution, but it appears to be causing an issue for permalinks for pages. All pages are giving a 404 not found. Any idea why this might be? –  Dustin Aug 1 '13 at 19:43
    
@Dustin I just tried it out again, and you're right. It was working fine when I wrote this snippet, I'll try and see what is causing it to break –  bungeshea Aug 3 '13 at 7:40
add comment

You can add a custom slug to the permalinks via plugin and use this inside your custom permalinks; dont forgett to update the permalinks. It is important, that the rules was refreshed. The follow source is a exmaple for use post format with the string %postformat%, but not perfectly code. Better is to load on hook plugins_loaded and not create a new class, but enough to test and use it now.

Please check this, current untested. I have write this for long time.

<?php
/**
 * Plugin Name: Post Format Permalink
 * Description: Allow to use Post Format as Permalink; inlcude %postformat% in custom permalink
 */

class Fb_Add_Post_Format_Permalink {

    function __construct() {

        add_filter( 'pre_post_link', array( $this, 'generate_permalink' ), 10, 2 );
        add_filter( 'post_rewrite_rules', array( $this, 'rewrite_rules' ) );
        add_filter( 'generate_rewrite_rules', array( $this, 'generate_rewrite_rules' ) );
    }

    function generate_permalink( $permalink, $post ) {
        global $standard_slug;

        if ( FALSE === strpos( $permalink, '%postformat%' ) )
            return $permalink;

        if ( ! is_object( $post ) )
            $post = get_post( $post_id );

        $postformat = get_post_format( $post->ID );
        if ( empty( $postformat ) )
            $postformat = ! empty($standard_slug) ? $standard_slug : 'standard';

        return str_replace( '%postformat%', $postformat, $permalink );
    }

    function generate_rewrite_rules( $wp_rewrite ) {
        global $clean_post_rewrites;

        $wp_rewrite->rules = $wp_rewrite->rules + $clean_post_rewrites;
    }

    function rewrite_rules( $post_rewrite ) {
        global $clean_post_rewrites, $wp_rewrite, $standard_slug;

        $wp_rewrite->use_verbose_page_rules = TRUE;

        $post_format_slugs = implode( '|', get_post_format_slugs() );
        if ( ! empty($standard_slug) )
            $post_format_slugs = preg_replace( '|standard|', $standard_slug, $post_format_slugs, 1 );

        while ( list($k, $v) = each( $post_rewrite ) ) {
            $new_k = preg_replace( '|%postformat%|', '(' . $post_format_slugs . ')', $k, 1 );
            $clean_post_rewrites[$new_k] = $v;
        }

        return $post_rewrite;
    }

}
$post_format = new Fb_Add_Post_Format_Permalink();
share|improve this answer
    
Sorry, that code does not work on my setup –  bungeshea Nov 1 '12 at 23:24
    
You have a error or nothing? –  bueltge Nov 1 '12 at 23:40
    
Permalinks appear to be http://example.com/%postformat%/example-quote-post –  bungeshea Nov 1 '12 at 23:52
    
Is the other solution not the right solution for you? –  bueltge Nov 5 '12 at 12:00
1  
Well, I appreciate that you were willing to help, so you can have the bounty. –  bungeshea Nov 6 '12 at 19:21
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.