WordPress Snippets at WPcustoms

Change permalink structure with a function

This snippet is a good start to see how we can programatically change the permalink structure. You can change it from the admin panel: settings -> permalinks but in case you need to do this in a plugin or use it for custom post types this gets you started.


/**
 * Snippet Name: Change permalink structure with a function
 * Snippet URL: https://wpcustoms.net/snippets/change-permalink-structure-function/
 */
  function wpc_custom_page_rules() {
    global $wp_rewrite;
    $wp_rewrite->page_structure = $wp_rewrite->root . 'pages/%pagename%.html'; // result: i.e. yoursite.com/pages/about.html
}
add_action( 'init', 'wpc_custom_page_rules' );