How To Add /blog/ in Front of Your Single Post URLs in WordPress

How to add -blog- in front of your single post URLs in WordPress
434 Views
4
(7)

Today, we’ll explain how to add /blog/ to the front of your single post URLs in WordPress. For example, let’s say your current post URL looks like this:
how-to-add-blog-in-front-of-your-single-post-urls-in-wordpress.

Now, you or your client might want it to look like this instead:
/blog/how-to-add-blog-in-front-of-your-single-post-urls-in-wordpress.

The good news is that this change is easy and can be made in just a few steps! Let’s walk you through how to make it happen.

<?php
add_filter( 'post_link', 'cxc_update_post_link', 1, 3 );

function cxc_update_post_link( $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_action( 'generate_rewrite_rules', 'cxcgenerate_rewrite_rules', 99, 1 );

function cxcgenerate_rewrite_rules( $wp_rewrite ) {
	$new_rules = array(
		'(.?.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[2]',
		'blog/([^/]+)/?$' => 'index.php?post_type=post&name=$matches[1]',
		'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]',
		'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1',
		'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
		'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
		'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]',     
		'blog/[^/]+/attachment/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
		'blog/[^/]+/embed/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
		'blog/([^/]+)/embed/?$' => 'index.php?post_type=post&name=$matches[1]&embed=true',
		'blog/[^/]+/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
		'blog/([^/]+)/trackback/?$' => 'index.php?post_type=post&name=$matches[1]&tb=1',
		'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]',
		'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]',
		'blog/page/([0-9]{1,})/?$' => 'index.php?post_type=post&paged=$matches[1]',
		'blog/[^/]+/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]',
		'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]',
		'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&cpage=$matches[2]',
		'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?post_type=post&name=$matches[1]&page=$matches[2]',
		'blog/[^/]+/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]',
		'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1',
		'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
		'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
		'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]',
	);
	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
?>

How useful was this blog?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 7

No votes so far! Be the first to rate this blog.

As you found this blog useful...

Follow us on social media!

We are sorry that this blog was not useful for you!

Let us improve this blog!

Tell us how we can improve this blog?

Leave a comment

Your email address will not be published. Required fields are marked *