Search code examples
wordpress.htaccesswordpress-themingpermalinks

Forcing Custom Permalinks in WordPress


Programmatically, without me overwriting the .htaccess file, what's the trick to tell WordPress to switch on custom permalinks (like %postname%) so that it creates the special .htaccess file that gives us pretty URLs? I need this for a special theme I'm coding.

EDIT: Note, this isn't a sysop or end user question. It's a question directed at WordPress programmers. Read the question below from @anubhava below and you'll see my answer.


Solution

  • You can change the permalink structure via your functions.php theme file by doing the following

    function change_permalinks() {
        global $wp_rewrite;
        $wp_rewrite->set_permalink_structure('/%category%/%postname%/');
        $wp_rewrite->flush_rules();
    }
    add_action('init', 'change_permalinks');