Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created September 12, 2016 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/d7faa44fa274ee4ae3983c63e24e2b7f to your computer and use it in GitHub Desktop.
Save kellenmace/d7faa44fa274ee4ae3983c63e24e2b7f to your computer and use it in GitHub Desktop.
Fix Edit Slug Button Missing Issue in WordPress
<?php
/**
* Changes all post links to the format 'newsroom/news/yyyy/mm/dd/%postname%'
*
* @param string $url The post URL.
* @param WP_Post $post The post.
* @return string $url The modified URL.
*/
function km_filter_post_links( $url, $post ) {
if ( 'post' === $post->post_type ) {
// If $url contains %postname% placeholder
if ( false !== strpos( $url, '%postname%' ) ) {
$slug = '%postname%';
} elseif ( $post->post_name ) {
$slug = $post->post_name;
} else {
$slug = sanitize_title( $post->post_title );
}
$date = DateTime::createFromFormat( 'Y-m-d H:i:s', $post->post_date )->format( 'Y/m/d' );
$url = home_url( user_trailingslashit( 'newsroom/news/' . $date . '/' . $slug ) );
}
return $url;
}
add_filter( 'post_link' , 'km_filter_post_links', 10, 2 );
/**
* Filter posts preview links to point to the correct URL.
*
* @param string $preview_link The preview link URL.
* @param WP_Post $post The post.
* @return string $url The modified link URL.
*/
function km_filter_post_preview_link( $preview_link, $post ) {
if ( 'post' === $post->post_type ) {
$preview_link = add_query_arg( array(
'p' => $post->ID,
'preview' => 'true',
), home_url( '/' ) );
}
return $preview_link;
}
add_filter( 'preview_post_link', 'km_filter_post_preview_link', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment