Recently, a client approached us with a specific WordPress URL customization request. They wanted us to Modify WordPress Post URL structure for their regular blog posts by adding “/news/” to the URLs. The goal was to transform URLs like “example.com/hydrogen-investment/” into “example.com/news/hydrogen-investment/” – but only for the default WordPress “post” post type.
This project required two key components: modifying how WordPress generates new post URLs and handling redirects for existing content. Here’s how we solved it.
1. First, we implemented a permalink modification function that adds “/news/” to all post URLs as they’re generated:
function modify_post_type_args($args, $post_type) {
if ($post_type !== 'post') {
return $args;
}
$args['rewrite'] = [
'slug' => 'news',
'with_front' => true,
];
return $args;
}
add_filter('register_post_type_args', 'modify_post_type_args', 10, 2);
function modify_post_permalink($permalink, $post) {
if ($post->post_type !== 'post') {
return $permalink;
}
return '/news/%postname%/';
}
add_filter('pre_post_link', 'modify_post_permalink', 10, 2);
The code above will modify WordPress post URLs.
2. Now to ensure existing links wouldn’t break, we added a redirect function:
/**
* Redirect old post URLs to include /news/
*/
function redirect_old_post_urls() {
// Don't redirect admin pages
if (is_admin()) {
return;
}
// Get current URL path
$current_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Don't redirect if already has /news/
if (strpos($current_path, '/news/') !== false) {
return;
}
// Remove trailing slash for consistency
$path = trim($current_path, '/');
// Skip if empty path
if (empty($path)) {
return;
}
// Check if this path corresponds to a post
$potential_post = get_page_by_path($path, OBJECT, 'post');
// Only redirect if this is a post type of 'post'
if ($potential_post && $potential_post->post_type === 'post') {
$new_url = home_url('/news/' . $path . '/');
// Perform 301 (permanent) redirect
wp_redirect($new_url, 301);
exit;
}
}
// Add the redirect action
add_action('template_redirect', 'redirect_old_post_urls');
3. Flush Rewrite Rules:
After adding this code, you may need to flush the rewrite rules to ensure the new permalink structure is recognized. You can do this by visiting the Permalinks settings page in the WordPress admin (Settings > Permalinks) and clicking “Save Changes”.
How it works
The solution works through three WordPress hooks:
register_post_type_args
andpre_post_link
filters to modify how new URLs are generatedtemplate_redirect
action to handle redirects from old URLs
The key feature of this implementation is its specificity – it only affects the default “post” post type, leaving pages, custom post types, and other content unchanged. This targeted approach meant our client could reorganize their news content URLs without disrupting the rest of their site structure.
Both functions include careful checks to prevent unnecessary processing: they verify the post type, avoid modifying URLs that already contain “/news/”, and handle edge cases like admin pages and empty paths.
if you would like to know more how to use the filter check out the WordPress documentation about pre_post_link.
In every project, we strive to deliver tailored, efficient solutions that address our clients’ unique needs while maintaining the integrity of their websites. Whether it’s customizing WordPress functionality, enhancing URL structures, or implementing complex redirects, we leverage our expertise to make it happen seamlessly. If you have a specific WordPress challenge, our team is ready to collaborate and craft a solution that aligns with your goals. Let us help you unlock your site’s full potential.