Simplifying Custom Post Type And Taxonomy Permalinks In WordPress
The Problem with Default Permalinks
The default permalink structure in WordPress utilizes complex query strings that contain excessive information about the custom post type or taxonomy term. For example, a default custom post type permalink may appear as:
http://example.com/?post_type=products&p=123
Likewise, default taxonomy term links end up convoluted and lengthy:
http://example.com/?taxonomy=product_cat&term=widgets
These default WordPress permalinks create URLs that are not user or search engine friendly. The complex query strings are difficult to interpret, do not contain relevant keywords, and lack hierarchy to indicate taxonomic relationships.
In addition, the default permalink structure provides little control or customization for the site owner. There is no way to optimize or simplify the permalinks without manual editing.
Understanding the Permalink API
Luckily, the WordPress permalink API provides a robust set of functions to programmatically create custom permalink structures.
The main functions that comprise the API include:
- register_post_type() – for registering custom post types
- register_taxonomy() – for registering custom taxonomies
- add_permastruct() – for defining permalink formats
Utilizing these functions in combination allows complete control over the permalinks for any custom post type or taxonomy registered in WordPress.
For example, the add_permastruct function allows you to define placeholders representing the post name, taxonomy name, taxonomy terms, post ID, and more.
Let’s see how these functions can be implemented to customize and simplify permalinks.
Customizing Post Type Permalinks
Custom post types registered in WordPress can have their permalink structure edited for simplification and optimization.
For example, to register a “products” custom post type with a simplified permalink structure, we can use:
function create_post_type() { $labels = array( 'name' => 'Products', ); $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, ); register_post_type( 'products', $args ); add_permastruct('products', '/products/%products%', array( '%products%' => '%postname%', ) ); } add_action( 'init', 'create_post_type');
Breaking this down:
- First we register the custom post type “products”
- Then we hook into the ‘init’ action to define the permalink structure using add_permastruct()
- We specify the structure to be /products/, followed by the post name
Now any products post will have a simplified permalink structure of:
http://example.com/products/sample-product
The post name slug helps identify the semantic topic, while the /products base indicates the custom post type section.
Additionally, you could include the post ID and use permalink structures like:
http://example.com/products/123/sample-product
This adds further uniqueness and specification to each post permalink.
Customizing Taxonomy Permalinks
In the same way, custom taxonomies registered with WordPress can also have their permalink format edited:
function create_taxonomy() { $labels = array( 'name' => 'Product Categories', ); register_taxonomy( 'product_cat', array( 'products' ), array( 'label' => __( 'Product Categories' ), 'rewrite' => array( 'slug' => 'products' ), 'hierarchical' => true, ) ); add_permastruct('product_cat', '/topics/%product_cat%', array( '%product_cat%' => '%product_cat%', )); } add_action( 'init', 'create_taxonomy');
Explaining the key parts:
- We register a taxonomy called “product_cat” for the “products” post type
- Using add_permastruct(), we define the permalink structure
- This makes the format /topics/ followed by the taxonomy term
Now taxonomy terms will have simplified permalinks like:
http://example.com/topics/widgets
The /topics base indicates the taxonomy section, while the term provides greater semantic specificity.
Additional Permalink Tips and Tricks
Further customization options are available when defining custom permalink structures in WordPress:
- Use rewrite rules for more advanced formatting of permalinks
- Set category or tag bases for blog posts
- Redirect default WordPress permalinks
- Integrate queries into permalinks with add_rewrite_tag()
There are also various plugins that can assist with simplifying permalinks:
- Custom Permalinks
- Permalink Manager Pro
- Taxonomy Permalinks
These plugins provide admin settings and GUI options for customizing permalinks without needing to manually edit code.
Summary
In summary, WordPress provides a host of options for customizing permalinks through its comprehensive Permalink API.
Leveraging these built-in WordPress functions allows complete control over the permalink format for custom post types and taxonomies.
Implementing custom permalinks provides simplified, semantic URLs optimized for users and search engines rather than complex default query strings.
Understanding the Permalink API is key to unlocking the potential for permalink customization in any WordPress project.