How to Add a Custom Slug to the URL Structure of WordPress Posts

In WordPress, customizing the URL structure of your posts can improve organization and search engine optimization (SEO). By default, WordPress applies the same permalink structure to all post types, including custom post types. However, in certain scenarios, you may require a different URL structure or exclude specific post types from the custom slug. In this tutorial, we will guide you through the process of adding a custom slug to the URL structure of posts in WordPress while addressing these considerations. This method allows you to easily customize the permalinks without using plugins.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Open Permalink Settings and Define Custom Slug

Once logged in, navigate to “Settings” in the WordPress sidebar menu and click on “Permalinks“. In the Permalink Settings page, select the “Custom Structure” option. This allows you to define a custom permalink structure In the Custom Structure field, enter the desired structure for your posts. For example, you can use “/blog/%postname%/” to add a “/blog/” prefix to the URL structure. This will result in URLs like ““.

Click the “Save Changes” button at the bottom of the page to save your permalink settings.

Open the “functions.php” File

Next, open the “functions.php” file of your WordPress theme in a code editor. You can find this file in the theme directory, typically located at “wp-content/themes/your-theme-name/functions.php“.

Add the Custom Slug Function

Inside the “functions.php” file, add the following code snippet:

function change_post_types_slug( $args, $post_type ) {
	// Check if the post type is not equal to "post"
	if ( 'post' !== $post_type ) {
		// Modify the rewrite slug for custom post types
		$args['rewrite'] = array(
			'with_front' => false, // Set to false to exclude the custom slug from the front of the permalink structure
			'slug'       => $post_type, // Assign the custom post type's name as the slug
	return $args;
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );
Code language: PHP (php)

This code snippet modifies the slugs in the URLs of custom post types with a detail page. In our case, we’re focusing on the default “post” post type.

The change_post_types_slug function is a custom function that is hooked into the register_post_type_args filter hook. This allows us to modify the post type arguments during registration.

Inside the function, we use a conditional statement (if) to check if the post type is not equal to “post”. This condition ensures that the modification is only applied to custom post types, not the default “post” post type.

If the condition is true, we modify the rewrite rules for the custom post type. The 'with_front' => false setting ensures that the custom slug is not added to the front of the permalink structure. The 'slug' => $post_type setting assigns the custom post type’s name as the slug, which allows each custom post type to retain its own URL structure.

By implementing this code snippet, the custom slug (“/blog/”) will only be applied to the default “post” post type, while other custom post types used for different purposes will maintain their own URL structure without the “/blog/” prefix.

Save the Changes and Test the New URL Structure

Save the modifications made to the “functions.php” file. Now, navigate to one of your posts and check the URL structure. It should now include the custom slug you defined, such as “/blog/“. For example, if you set the custom slug as “blog”, a post titled “Sample Post” would have a URL like ““.

That’s it! You have successfully added a custom slug to the URL structure of posts in WordPress. By using the built-in permalink settings and the provided code snippet, you can easily customize the permalinks for your posts without the need for plugins. Remember to save your changes and test the new URL structure to ensure everything is working as expected.

Note: When modifying the permalink structure, it is important to set up proper redirects for existing content to avoid broken links and maintain SEO rankings.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

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