Guest User

Untitled

a guest
Jun 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. http://example.com/link/look-at-this-cool-site/
  2.  
  3. http://example.com/quote/example-quote-post
  4.  
  5. http://example.com/just-another-post
  6.  
  7. <?php
  8. /**
  9. * Plugin Name: Post Format Permalink
  10. * Plugin URI: https://wordpress.stackexchange.com/q/70627/19726
  11. * Description: Include the post format slug in your permalinks. Simply use the <code>%post_format%</code> tag as part of your custom permalink.
  12. * Version: 1.2
  13. * Author: shea
  14. * Author URI: https://wordpress.stackexchange.com/users/19726
  15. */
  16.  
  17. add_filter( 'post_link', 'post_format_permalink', 10, 2 );
  18. add_filter( 'post_type_link', 'post_format_permalink', 10, 2 );
  19.  
  20. function post_format_permalink( $permalink, $post_id ) {
  21.  
  22. // if we're not using the %post_format% tag in our permalinks, bail early
  23. if ( strpos($permalink, '%post_format%') === FALSE ) return $permalink;
  24.  
  25. // get the post object
  26. $post = get_post( $post_id );
  27. if ( ! $post ) return $permalink;
  28.  
  29. // get post format slug
  30. $format = get_post_format( $post->ID );
  31.  
  32. // set the slug for standard posts
  33. if ( empty( $format ) )
  34. $format = apply_filters( 'post_format_standard_slug', 'standard' );
  35.  
  36. // apply the post format slug to the permalink
  37. return str_replace( '%post_format%', $format, $permalink );
  38. }
  39.  
  40. <?php
  41. /**
  42. * Plugin Name: Post Format Permalink
  43. * Description: Allow to use Post Format as Permalink; inlcude %postformat% in custom permalink
  44. */
  45.  
  46. class Fb_Add_Post_Format_Permalink {
  47.  
  48. function __construct() {
  49.  
  50. add_filter( 'pre_post_link', array( $this, 'generate_permalink' ), 10, 2 );
  51. add_filter( 'post_rewrite_rules', array( $this, 'rewrite_rules' ) );
  52. add_filter( 'generate_rewrite_rules', array( $this, 'generate_rewrite_rules' ) );
  53. }
  54.  
  55. function generate_permalink( $permalink, $post ) {
  56. global $standard_slug;
  57.  
  58. if ( FALSE === strpos( $permalink, '%postformat%' ) )
  59. return $permalink;
  60.  
  61. if ( ! is_object( $post ) )
  62. $post = get_post( $post_id );
  63.  
  64. $postformat = get_post_format( $post->ID );
  65. if ( empty( $postformat ) )
  66. $postformat = ! empty($standard_slug) ? $standard_slug : 'standard';
  67.  
  68. return str_replace( '%postformat%', $postformat, $permalink );
  69. }
  70.  
  71. function generate_rewrite_rules( $wp_rewrite ) {
  72. global $clean_post_rewrites;
  73.  
  74. $wp_rewrite->rules = $wp_rewrite->rules + $clean_post_rewrites;
  75. }
  76.  
  77. function rewrite_rules( $post_rewrite ) {
  78. global $clean_post_rewrites, $wp_rewrite, $standard_slug;
  79.  
  80. $wp_rewrite->use_verbose_page_rules = TRUE;
  81.  
  82. $post_format_slugs = implode( '|', get_post_format_slugs() );
  83. if ( ! empty($standard_slug) )
  84. $post_format_slugs = preg_replace( '|standard|', $standard_slug, $post_format_slugs, 1 );
  85.  
  86. while ( list($k, $v) = each( $post_rewrite ) ) {
  87. $new_k = preg_replace( '|%postformat%|', '(' . $post_format_slugs . ')', $k, 1 );
  88. $clean_post_rewrites[$new_k] = $v;
  89. }
  90.  
  91. return $post_rewrite;
  92. }
  93.  
  94. }
  95. $post_format = new Fb_Add_Post_Format_Permalink();
Add Comment
Please, Sign In to add comment