CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   WordPress   How to Remove Slug From Custom Post Type in WordPress Withou ...

How to Remove Slug From Custom Post Type in WordPress Without Plugin ?

May 4, 2017 by SNK

remove slug from custom post type in wordpress

Today in this tutorial we are going to see how to remove slug from custom post type in WordPress without plugin.

remove slug from custom post type in wordpress

I have been trying to remove the slug from custom post type because i want my slug to be shorter.There is a old fashioned way of removing the slug in custom type of WordPress by using the following code.

Take a look at it, now it no longer works in most recent versions of WordPress after 3.5 installs.

functions.php
PHP
1
'rewrite'   => array( 'slug' => true, 'with_front' => true),

Change above line to

functions.php
PHP
1
'rewrite'   => array( 'slug' => false, 'with_front' => false),

But now, it wont work adding only these piece of code in functions.php file because, we have to keep in mind that it will bring conflict if slug of custom post type and page or post may become same.

So what we will do is first remove the slug from the permalink by adding this single piece of code in functions.php.

functions.php
PHP
1
2
3
4
5
6
7
8
9
10
11
function remove_cpt_slug( $post_link, $post, $leavename ) {
 
    if ( 'your-cpt-slug' != $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', 'remove_cpt_slug', 10, 3 );

And now if you visit the page you will get Page Not Found Error because its not enough.

Why ?

Because WordPress behave only post and page to work this way.

To make it work we need to add certain line of codes to tell WordPress to behave custom post type as page or post. To do this add these lines of codes in functions.php to make it work for custom post type as well.

functions.php
1
2
3
4
5
6
7
8
9
10
11
function change_slug_struct( $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', 'your-cpt-slug', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'change_slug_struct' );

Now go to Settings -> Permalinks and save changes without making any other changes in the permalinks section and you are good to go. Check your custom post type posts your slug have been removed from custom post type permalink structure.

If you have any other problem you can contact us via contact form from the contact us page. We will be happy to sort it out.

SHARE ON
Buffer

Enjoyed this article?

Like us on

WordPress custom post type permalink structure remove slug from custom post type plugin remove slug wordpress plugin

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

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

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.