Post slug cannot be edited after add add_rewrite_rule
Question
Because I wanted to add a blog reference to the slug of each post, I add a add_rewrite_rule.
But now the slug can not be edited on the dashboard.
How can I make the slug editable?
my dashbord:
my code:
// Add /blog/ to all blog pages.
class BlogPage{
const SLUG_BLOG = 'blog';
public static function init() {
self::wpa_fix_blog_pagination();
}
public static function wpa_fix_blog_pagination(){
// Fix pagination
add_rewrite_rule(
'blog/page/([0-9]+)/?$',
'index.php?pagename=blog&paged=$matches[1]',
'top'
);
// Change the default 'post' and add /blog/ to permalink.
add_rewrite_rule(
self::SLUG_BLOG.'/([^/]*)$',
'index.php?name=$matches[1]',
'top'
);
add_rewrite_tag('%'.self::SLUG_BLOG.'%','([^/]*)');
// Change the default permalink structure of tags and categories
global $wp_rewrite;
$wp_rewrite->extra_permastructs['category']['struct'] = '/' . self::SLUG_BLOG .'/category/%category%';
$wp_rewrite->extra_permastructs['post_tag']['struct'] = '/' . self::SLUG_BLOG .'/tag/%post_tag%';
// Change the default permalink structure of authors
$wp_rewrite->author_base = 'blog/author';
$wp_rewrite->author_structure = '/' . $wp_rewrite->author_base . '/%author%';
add_rewrite_rule(self::SLUG_BLOG .'/author/([^/]+)/?$', 'index.php?author_name=$matches[1]', 'top');
add_filter( 'post_link', array(__CLASS__,'change_post_link'), 10, 3 );
}
public static function change_post_link($url, $post, $leavename){
if ( $post->post_type == 'post' ) {
$url = home_url( user_trailingslashit( self::SLUG_BLOG . '/' . $post->post_name ) );
}
return $url;
}
}
// Init Blog Class
add_action("init",function(){
BlogPage::init();
});
function fix_guid( $guid ) {
$guid = get_permalink( get_the_ID() );
return $guid;
}
add_filter( 'get_the_guid', 'fix_guid' );
// redirect_posts_to_newblogurl if the post not starts with /blog/
add_action('template_redirect', function() {
$lang = $_COOKIE["wp-wpml_current_language"];
if (is_singular('post') && $lang == 'en') {
if (substr( $_SERVER['REQUEST_URI'], 0, 6 ) !== "/blog/") {
wp_redirect( get_the_permalink(), 301 );
exit;
}
}
});
0
2 months
0 Answers
14 views
0
Leave an answer
You must login or register to add a new answer .