vapvarun

Insert Blog slug for all blog post

Apr 7th, 2021
343
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wb_create_new_url_querystring() {
  2.     add_rewrite_rule(
  3.         'blog/([^/]*)$',
  4.         'index.php?name=$matches[1]',
  5.         'top'
  6.     );
  7.  
  8.     add_rewrite_tag('%blog%','([^/]*)');
  9. }
  10. add_action('init', 'wb_create_new_url_querystring', 999 );
  11.  
  12.  
  13. /**
  14.  * Modify post link
  15.  * This will print /blog/post-name instead of /post-name
  16.  */
  17. function wb_append_query_string( $url, $post, $leavename ) {
  18.  
  19.     if ( $post->post_type != 'post' )
  20.             return $url;
  21.    
  22.    
  23.     if ( false !== strpos( $url, '%postname%' ) ) {
  24.             $slug = '%postname%';
  25.     }
  26.     elseif ( $post->post_name ) {
  27.             $slug = $post->post_name;
  28.     }
  29.     else {
  30.         $slug = sanitize_title( $post->post_title );
  31.     }
  32.    
  33.     $url = home_url( user_trailingslashit( 'blog/'. $slug ) );
  34.  
  35.     return $url;
  36. }
  37. add_filter( 'post_link', 'wb_append_query_string', 10, 3 );
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×