Optimizing WordPress blog permalinks by adding “/blog/” to your URLs can streamline site structure, improve SEO, and enhance user experience. This guide offers a step-by-step method to customize your WordPress blog permalinks, ensuring your blog posts are clearly organized without affecting other content types on your website.
1. Introduction to WordPress Blog Permalinks
WordPress blog permalinks are the permanent URLs used for blog posts, helping users and search engines understand site structure and content. Customizing these permalinks is an effective way to organize posts, support SEO, and ensure a smoother user experience.
2. Benefits of Adding “/blog/” to WordPress Blog Permalinks
By adding “/blog/” to your WordPress blog permalinks, you can:
- Improve search engine optimization by indicating blog-specific content.
- Make your URLs more intuitive and organized, enhancing site navigation.
- Clearly distinguish blog posts from other custom content types, such as services or galleries.
3. How WordPress Manages Blog Permalink Structure by Default
WordPress offers various permalink structures like:
/%postname%/
(post title only)/category/postname/
However, adding a unique base like “/blog/” specifically for blog posts requires additional setup and customization using plugins and custom code to ensure other post types aren’t affected.
4. Step 1: Configuring Blog Permalinks in Settings
To begin customizing your blog permalinks:
- Go to Settings > Permalinks in your WordPress dashboard.
- Select “Post name” (
/%postname%/
) for a clean URL structure. - Save the changes, setting the foundation for adding “/blog/” to your blog post URLs.
5. Step 2: Using WPCode Lite for Custom Code Insertion
To safely add custom PHP code to WordPress, use the WPCode Lite plugin:
- Install WPCode Lite from the WordPress plugin repository.
- Activate the plugin from your dashboard.
- WPCode Lite allows you to insert PHP snippets, making it a reliable tool for customizing WordPress blog permalinks without modifying theme files.
6. Step 3: Setting Up Custom Post Type UI Plugin for Other Content Types
For websites with custom post types like “Gallery” or “Services,” the Custom Post Type UI plugin is invaluable:
- Install and activate Custom Post Type UI.
- Go to CPT UI > Add/Edit Post Types.
- Set up additional post types, such as “Gallery”
7. Step 4: Adding Custom PHP Code in WPCode Lite
Once the plugins are ready, add PHP code to WPCode Lite to implement “/blog/” in your WordPress blog permalinks:
- Open WPCode Lite and create a new PHP snippet, then paste the following code:
function THEME_SLUG_posts_add_rewrite_rules( $wp_rewrite ) { $new_rules = [ 'blog/page/([0-9]{1,})/? => 'index.php?post_type=post&paged='. $wp_rewrite->preg_index(1), 'blog/(.+?)/? => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1), ]; $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; return $wp_rewrite->rules; } add_action('generate_rewrite_rules', 'THEME_SLUG_posts_add_rewrite_rules'); function THEME_SLUG_posts_change_blog_links($post_link, $id=0){ $post = get_post($id); if( is_object($post) && $post->post_type == 'post'){ return home_url('/blog/'. $post->post_name.'/'); } return $post_link; } add_filter('post_link', 'THEME_SLUG_posts_change_blog_links', 1, 3);
- Save and activate the snippet. This code effectively adds “/blog/” to WordPress blog permalinks for all blog posts, creating a distinct, SEO-friendly URL structure.
8. Understanding the PHP Code of above Permalink Rules
The PHP code of above permalinks does two main things:
- Rewrite Rules: The
THEME_SLUG_posts_add_rewrite_rules()
function establishes rules to redirect URLs under “/blog/” directly to blog posts. - Custom Post Links: The
THEME_SLUG_posts_change_blog_links()
function appends the “/blog/” prefix to post URLs, ensuring a unique, user-friendly permalink structure.
9. Separating Custom Post Types from Blog Permalinks
Using Custom Post Type UI ensures WordPress blog permalinks remain exclusive to blog posts. Custom post types like “Gallery” or “Services” will retain their unique structures, making site organization clearer and more manageable.
10. Testing and Finalizing the WordPress Permalink Setup
- Flush Rewrite Rules by visiting Settings > Permalinks and clicking “Save Changes.”
- Test URLs to confirm blog posts are under “/blog/” while custom post types maintain their distinct paths.
- Ensure your SEO plugin, like Yoast SEO or All in One SEO, aligns with your WordPress blog permalinks setup to avoid any negative SEO impact.
Customizing WordPress blog permalinks by adding “/blog/” to URLs not only enhances site structure but also supports SEO and user experience. With plugins like WPCode Lite and Custom Post Type UI, alongside custom PHP code, you can create a well-organized, SEO-friendly WordPress site that guides users through your blog content intuitively.
By following this setup, your WordPress blog permalinks will be organized, clearly separated from other content types, and optimized for search engines, making your site easier for both visitors and search engines to navigate.