Using add_permastruct to create custom permalink structures in WordPress

The add_permastruct function in WordPress is used to add a custom permalink structure for a specific type of content. This function allows developers to define how URLs for certain content types should be formatted, providing a way to create more readable and SEO-friendly URLs.

When a new custom content type is registered, add_permastruct can be employed to specify the permalink structure for that content type. This function helps in managing the URL patterns for custom post types, taxonomies, and other custom content, ensuring that they follow a consistent and desired format.

The function integrates with WordPress’s rewrite rules, which are responsible for interpreting the URLs and directing them to the appropriate content. By defining custom permalink structures, developers can improve the organization and accessibility of content on a WordPress site.

The add_permastruct function is typically used in conjunction with other functions that register custom content types, ensuring that the URLs for these types are properly formatted and recognized by WordPress’s routing system.

Parameters

  • $name (string), required. Name for permalink structure.
  • $struct (string), required. Permalink structure.
  • $args (array), optional. Default value: array(). Arguments for building the rules from the permalink structure, see WP_Rewrite::add_permastruct() for full details.

Return Value

The add_permastruct function does not return a value.

Examples

How to Add a Custom Permalink Structure for a Custom Post Type

function custom_post_type_permastruct() {
 // Add custom permalink structure for 'product' post type
 add_permastruct(
 'product', // Name for permalink structure
 '/product/%product%', // Permalink structure
 array(
 'with_front' => false, // Whether the structure should be prepended with the front base
 'ep_mask' => EP_PERMALINK // Endpoint mask for the structure
 )
 );
}
add_action('init', 'custom_post_type_permastruct');

This code snippet demonstrates how to use the add_permastruct function to add a custom permalink structure for a custom post type named ‘product’. The $name parameter is set to ‘product’, the $struct parameter defines the URL structure as ‘/product/%product%’, and the $args array includes options such as with_front (whether to prepend the front base) and ep_mask (endpoint mask).

How to Add a Custom Permalink Structure with Pagination

function custom_permastruct_with_pagination() {
 // Add custom permalink structure with pagination
 add_permastruct(
 'custom-pagination', // Name for permalink structure
 '/custom/%custom%/page/%paged%', // Permalink structure
 array(
 'paged' => true, // Enable pagination
 'with_front' => false, // Whether the structure should be prepended with the front base
 'ep_mask' => EP_PAGES // Endpoint mask for the structure
 )
 );
}
add_action('init', 'custom_permastruct_with_pagination');

This code snippet demonstrates how to use the add_permastruct function to add a custom permalink structure with pagination. The $name parameter is set to ‘custom-pagination’, the $struct parameter defines the URL structure as ‘/custom/%custom%/page/%paged%’, and the $args array includes options such as paged (enabling pagination), with_front (whether to prepend the front base), and ep_mask (endpoint mask).

Conclusion

The add_permastruct function in WordPress provides developers with the capability to define custom permalink structures for various content types. By leveraging this function, it is possible to create more readable and SEO-friendly URLs tailored to specific requirements. The function is particularly useful when custom post types or taxonomies need unique URL patterns that differ from the default WordPress permalink structures. Through the use of add_permastruct, developers can enhance the flexibility and customization of their WordPress sites, ensuring that URLs are both user-friendly and aligned with the site’s content strategy.

Related WordPress Functions