Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // REDIRECT /%category%/%postname%/ to /%postname%/ (for old posts links in social, after change permalinks)
- add_filter( '404_template', 'custom_redirect_to_category' );
- function custom_redirect_to_category($template) {
- if ( ! is_404() ){
- return $template;
- }
- global $wp_rewrite;
- global $wp_query;
- if ( '/%category%/%postname%/' !== $wp_rewrite->permalink_structure ){
- return $template;
- }
- if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) ){
- return $template;
- }
- $permalink = get_permalink( $post->ID );
- wp_redirect( $permalink, 301 );
- exit;
- }
Add Comment
Please, Sign In to add comment