In WordPress 2.9 custom post type was introduced, that opened a new world for many WordPress developers. I’ve come across a painful issue when it comes to removing the slug and not destroying the rewrite rules for other post types. This only concerns those who are using %postname% as permalink structure and want to add a custom post type so that the urls for them are in the same way as normal posts and pages: http://siteurl/%custom_post_type_title%/.
Add custom post type
In this example I’ll add “books” as a custom post type. What we want to achieve is that the urls are http://siteurl/%bookname%/
First we need to have a closer look at what rewrite options we have when we register a new post type with register_post_type.
For my hack to work you need to add a slug and set with_front to false:
array("slug" => "/book", "with_front" => false); |
Don’t set the “slug” to “/”, because it overrides other rules and gives post/pages a 404 when you try to access them.
Step 1: Register post type
First we will start with registering our new post type
function register_book_type() { register_post_type('books', array( 'labels' => array( 'name' => __( 'Books' ), 'singular_name' => __( 'Book' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New Book' ), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Book' ), 'new_item' => __( 'New Book' ), 'view' => __( 'View Book' ), 'view_item' => __( 'View Book' ), 'search_items' => __( 'Search Books' ), 'not_found' => __( 'No Books found' ), 'not_found_in_trash' => __( 'No Books found in Trash' ), 'parent' => __( 'Parent Book' ), ), 'public' => true, 'menu_position' => 5, 'show_ui' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array("slug" => "/book", "with_front" => false), 'query_var' => "book", 'supports' => array('title', 'editor', 'thumbnail') )); } add_action('init', 'register_book_type'); |
If you now add a book it will get the url http://siteurl/book/%post_title%/, so far so good.
Step 2: Add rewrite rule
We need to add a new rewrite rule to make the system recognise books without the slug /book/.
It’s important that you hook the rewrite rule after the theme setup is finished, otherwise it will be overwritten.
in this case, the regular expression to look for anything in the root will be “(.*?)$”, basically look for anything.
Our redirection is the query ‘index.php?book=$id’.
function book_rewrite_rule() { add_rewrite_rule('(.*?)$', 'index.php?book=$matches[1]', 'top'); } add_action('after_theme_setup', 'book_rewrite_rule'); |
Remember to flush your rules, 2 easy ways to do this. Access the Permalink page from WordPress Admin or call “flush_rewrite_rules” anywhere once from your theme or plugin.
flush_rewrite_rules(); |
This has to be done each time you change something in your permalink structure or rewrite rules.
Your book posts will now be accessible from http://siteurl/%post_title%/ as well as http://siteurl/book/%post_title%/ and we don’t want to have two links to the same post.
Step 3: .htaccess
Add a rule to redirect /book/%post_title%/ to /%post_title%/ in .htaccess
RewriteRule ^book/(.+)$ /$1 [R=301,L] |
Step 4: Fix the permalink that is shown when editing
When adding a new book in this case or editing an old one, the permalink will still show “Permalink: http://siteurl/book/%post_title%/” and View will also go to the url with the slug. Here is the final piece to hack the slug away
function remove_slug($permalink, $post, $leavename) { $permalink = str_replace(get_bloginfo('url') . '/book' , get_bloginfo('url'), $permalink); return $permalink; } add_filter('post_type_link', 'remove_slug', 10, 3); |
That’s all! I’m sure this could be done in a million ways but this is the only way I got it to work. Please comment if you find any difficulties or improvements.
Download the full plugin from from wordpress.org
Rank Tracker Wordpress Plugin
See which keywords are really bringing you traffic. Use the information to improve your sites income, download plugin
Themedy Thesis & Genesis Skin Club
Themedy is a premium skin marketplace offering quality child themes for the some of the most popular Wordpress frameworks like Thesis & Genesis. Read more

Pingback: some cool links which helps you to make you full programmer | myjunk.in