How to Remove Specific Character from WordPress URLs
Having clean and optimized URLs is an important aspect of search engine optimization (SEO) for your WordPress website. One common issue that WordPress users face is the inclusion of the post type in their URLs, which can make the URLs look cluttered and less user-friendly.
In this blog post, we'll explore a solution to remove the post type from your WordPress URLs, using the example of the "service" post type. By the end of this article, you'll know how to implement this solution and the benefits it can bring to your website's performance and user experience.
Understanding the Problem
By default, WordPress URLs for custom post types include the post type in the URL structure. For example, a post with the slug "my-service" on a "service" post type would have a URL like https://example.com/service/my-service
.
While this structure can be useful for organizing your content, it can also make your URLs look less clean and less SEO-friendly. Ideally, you'd want your URLs to look like https://example.com/my-service
.
The Solution: Removing the Post Type from URLs
To remove the post type from your WordPress URLs, you can use the following code snippet and add it to your theme's functions.php
file:
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'service' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'service', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );
Let's break down the code:
- The
na_remove_slug
function is a custom function that filters the post type link. It checks if the post type is "service" and the post status is "publish". If these conditions are met, it removes the post type from the URL.
- The
na_parse_request
function is another custom function that modifies the main query to include the "service" post type when a user accesses a URL with the post slug.
By adding this code to your functions.php
file, you'll be able to remove the "service" post type from your URLs, resulting in more clean and SEO-friendly URLs.
Benefits of Removing Post Type from URLs
Implementing this solution can bring several benefits to your WordPress website:
- Improved User Experience: Clean and concise URLs are more user-friendly and easier for your visitors to remember and share.
- Enhanced SEO: Search engines often place importance on URL structure, and having clean URLs can positively impact your website's search engine rankings.
- Better Branding: Removing the post type from your URLs can help create a more cohesive and professional-looking website, which can contribute to your overall brand identity.
- Increased Accessibility: Users with disabilities, such as those using screen readers, may find it easier to navigate your website when the URLs are more straightforward and less cluttered.
Considerations and Limitations
While the solution presented in this article is effective, there are a few things to consider:
- Existing URLs: If you have existing content with the post type in the URL, you may need to set up redirects to ensure that users are seamlessly redirected to the new, clean URLs. This can be done using a plugin like Redirection or by modifying your website's
.htaccess
file.
- Third-Party Plugins: If your website uses third-party plugins that interact with your post types, you may need to ensure that those plugins are compatible with the URL changes and make any necessary adjustments.
- Pagination and Archives: The
na_parse_request
function in the code snippet ensures that the post type is included in the main query, which is necessary for pagination and archives to work correctly.
Conclusion
Removing the post type from your WordPress URLs is a simple yet effective way to improve the overall user experience and SEO performance of your website. By following the steps outlined in this article, you can easily implement this solution and enjoy the benefits of clean, SEO-friendly URLs.
If you need further assistance or have any questions, feel free to reach out to the Flowpoint.ai team. We specialize in website optimization and can help you identify and fix technical issues that may be impacting your conversion rates
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.