Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Last active September 6, 2017 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximeCulea/18f99304a0ecc8b161ce8a82036ac160 to your computer and use it in GitHub Desktop.
Save MaximeCulea/18f99304a0ecc8b161ce8a82036ac160 to your computer and use it in GitHub Desktop.
Prefix page's slug like "/page/{slug}"
<?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