Use Custom Permalink Structure for Pages in WordPress

How to Use Custom Permalink Structure for Pages in WordPress

in Functions
Google+ LinkedIn

Permalinks are the permanent URLs to your individual blog posts, pages as well as categories, tags and other lists of blog postings. By default, WordPress uses post ID query string as permalink structure rule but it’s not very user-friendly, and it is also not Search Engine Friendly. It is better to refer a visitor to a URL such as http://www.yourwebsite.com/news-story/ than http://www.yourwebsite.com/?page_id=928923. That’s why WordPress comes with an option to change the default structure to pretty URLs and human readable. Most of the website owners use “/%postname%/” as their permalink structure because it generates short memorable URLs of post and pages.

WordPress use same permalink structure rule for all website entries and lists. But sometimes you may want to use custom permalink structure only for pages for better search engine optimization. For example if you use blog prefix for all blog posts (e.g. http://www.yourwebsite.com/blog/news-story/) and don’t want to use this prefix for pages, then you are out of luck because WordPress does not provide an easy way to do that. In this article we will show you how to change permalink structure rules for pages while keep using standard rules for blog posts and others items.

To define custom permalink structure rules for pages, simply paste this code snippet in your theme’s functions.php file. In this example, we are adding page prefix for pages followed by their title. This is just an example, but you should use different custom permalink structure rules, better suited for your website.

// use custom permalink structure for pages in wordpress
function custom_page_rules() {
    global $wp_rewrite;
    $wp_rewrite->page_structure = $wp_rewrite->root . 'page/%pagename%/';
}
add_action( 'init', 'custom_page_rules' );

By adding this code will not make pages to use new custom permalink structure and you will see 404 error pages. Don’t panic, everything is normal. Just visit permalink settings section in WordPress admin panel and WordPress will enforce new permalink structure rules.

Share the love

If you like this snippet, share it with others!

Leave a Reply

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