Last active
September 6, 2017 09:10
-
-
Save MaximeCulea/18f99304a0ecc8b161ce8a82036ac160 to your computer and use it in GitHub Desktop.
Prefix page's slug like "/page/{slug}"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change post type single slug | |
*/ | |
add_filter( 'register_post_type_args', 'bea_edit_page', 20, 2 ); | |
function bea_edit_page( $args, $post_type ) { | |
if ( 'page' !== $post_type ) { | |
return $args; | |
} | |
$args['rewrite']['slug'] = 'page'; | |
return $args; | |
} | |
/** | |
* Change page's link | |
*/ | |
add_filter( 'page_link', 'bea_page_rewrite', 20, 3 ); | |
function bea_page_rewrite( $link, $post_id, $sample ) { | |
if ( 'page' !== get_post_type( $post_id ) ) { | |
return $link; | |
} | |
$title = sanitize_title( get_the_title( $post_id ) ); | |
return str_replace( $title, sprintf( 'page/%s', $title ), $link ); | |
} | |
/** | |
* Handle duplicate content | |
*/ | |
add_action( 'template_redirect', 'bea_redirect' ); | |
function bea_redirect() { | |
if ( ! is_page() || ! strpos( '/page/', $_SERVER['REQUEST_URI'] ) ) { | |
return; | |
} | |
wp_safe_redirect( get_page_link(), '301' ); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment