?php /** * Blog CPT(new): /blogs/{slug}/ -> /blog/{slug}/ * Old URLs 301 redirect. Does not modify .htaccess. */ if ( ! defined( 'ABSPATH' ) ) { exit; } add_filter( 'register_post_type_args', function ( $args, $post_type ) { if ( 'new' !== $post_type ) { return $args; } $args['rewrite'] = array( 'slug' => 'blog', 'with_front' => false, ); $args['has_archive'] = true; return $args; }, 20, 2 ); add_action( 'init', function () { add_rewrite_rule( '^blog/([^/]+)/?$', 'index.php?post_type=new&name=$matches[1]', 'top' ); add_rewrite_rule( '^blog/page/([0-9]+)/?$', 'index.php?post_type=new&paged=$matches[1]', 'top' ); add_rewrite_rule( '^blog/?$', 'index.php?post_type=new', 'top' ); }, 30 ); add_filter( 'post_type_link', function ( $post_link, $post ) { if ( ! is_object( $post ) || 'new' !== $post->post_type ) { return $post_link; } return str_replace( '/blogs/', '/blog/', $post_link ); }, 100, 2 ); add_filter( 'custom_permalink_before_saving', function ( $permalink ) { if ( is_string( $permalink ) ) { $permalink = str_replace( array( '/blogs/', 'blogs/' ), array( '/blog/', 'blog/' ), $permalink ); } return $permalink; } ); /** * Resolve /blog/{slug}/ even when Custom Permalinks meta still has blogs/. */ add_filter( 'request', function ( $query ) { $path = trim( (string) parse_url( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '', PHP_URL_PATH ), '/' ); if ( 'blog' === $path ) { $query['post_type'] = 'new'; unset( $query['pagename'], $query['page'], $query['error'], $query['name'], $query['category_name'] ); return $query; } if ( ! preg_match( '#^blog/([^/]+)$#', $path, $m ) ) { return $query; } $slug = sanitize_title( $m[1] ); global $wpdb; $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'custom_permalink' AND meta_value IN (%s,%s,%s,%s) LIMIT 1", 'blog/' . $slug, 'blog/' . $slug . '/', 'blogs/' . $slug, 'blogs/' . $slug . '/' ) ); $post = $post_id ? get_post( (int) $post_id ) : get_page_by_path( $slug, OBJECT, 'new' ); if ( $post && 'new' === $post->post_type && 'publish' === $post->post_status ) { return array( 'post_type' => 'new', 'name' => $post->post_name, ); } return $query; }, 1 ); /** * 301: /blogs/... -> /blog/... */ add_action( 'template_redirect', function () { $path = trim( (string) parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' ); if ( ! preg_match( '#^blogs(?:/(.*))?$#', $path, $m ) ) { return; } $rest = isset( $m[1] ) ? trim( $m[1], '/' ) : ''; $dest = ( '' === $rest ) ? '/blog/' : user_trailingslashit( 'blog/' . $rest ); wp_redirect( home_url( $dest ), 301 ); exit; }, 0 ); add_filter( 'redirect_canonical', function ( $redirect_url, $requested_url ) { if ( $redirect_url && false !== strpos( $redirect_url, '/blogs/' ) && false !== strpos( $requested_url, '/blog/' ) ) { return false; } return $redirect_url; }, 10, 2 ); /** * One-time migration of Custom Permalinks meta + flush rewrites. */ add_action( 'init', function () { if ( get_option( 'newlight_blog_slug_v4' ) ) { return; } $ids = get_posts( array( 'post_type' => 'new', 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', ) ); foreach ( $ids as $id ) { $cp = get_post_meta( $id, 'custom_permalink', true ); if ( is_string( $cp ) && false !== strpos( $cp, 'blogs' ) ) { $new_cp = str_replace( array( '/blogs/', 'blogs/' ), array( '/blog/', 'blog/' ), $cp ); if ( $new_cp !== $cp ) { update_post_meta( $id, 'custom_permalink', $new_cp ); } } } $table = get_option( 'custom_permalink_table' ); if ( is_array( $table ) ) { $new_table = array(); foreach ( $table as $key => $val ) { $new_key = is_string( $key ) ? str_replace( array( '/blogs/', 'blogs/' ), array( '/blog/', 'blog/' ), $key ) : $key; $new_table[ $new_key ] = $val; } update_option( 'custom_permalink_table', $new_table ); } flush_rewrite_rules( false ); update_option( 'newlight_blog_slug_v4', 1 ); }, 99 ); https://www.xmnewlight.com/post-sitemap.xml 2026-07-21T02:40:33+00:00 https://www.xmnewlight.com/page-sitemap.xml 2026-07-20T14:29:18+00:00 https://www.xmnewlight.com/project-sitemap.xml 2023-09-22T08:53:54+00:00 https://www.xmnewlight.com/new-sitemap.xml 2026-07-24T03:56:04+00:00 https://www.xmnewlight.com/category-sitemap.xml 2026-06-04T06:14:20+00:00 https://www.xmnewlight.com/local-sitemap.xml 2026-03-11T01:36:07+00:00