Instantly share code, notes, and snippets.
Created
September 12, 2016 20:18
Fix Edit Slug Button Missing Issue in WordPress
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 | |
/** | |
* 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