I'm setting up a Wordpress Network and wanted all new sites to have the same permalink structure (i.e. "/%year%/%monthnum%/%postname%/"). I'm wondering if this is possible to do via hooks or hacks in functions.php, without having to rely on users to choose that structure.

3 Answers 3


You can set the permalink structure by calling on the set_permalink_structure() method of the global $wp_rewrite object.

add_action( 'init', function() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
} );

Here's a PHP < 5.3 version of the code in case you're getting errors.

function reset_permalinks() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
add_action( 'init', 'reset_permalinks' );
  • Copying and pasting your code generates an error, but the rationale does the trick! I only wonder if my question / intentions are good practice, though... Oct 16, 2011 at 21:26
  • 1
    Tomas, thanks for accepting the answer. Glad to have been of help. As for good practices - I think that if your objective is to enforce and lock such a permalink structure across websites - then that's justifiable; and by 'lock' I mean that code will not allow any changes to the structure via the Admin, if that's fine then I reckon it's totally okay to do this.
    – soulseekah
    Oct 17, 2011 at 4:41
  • 1
    It works but it cause a conflict when admin saves from permalink page (404 on posts). Then when the site loades (proccess init hook) the permalink structure changes again (404 on posts again). using $wp_rewrite->flush_rules(); solves the problem. Bad practice to use it on init and run it every time. Just visiting the permalinks page will do the tricks.
    – Sisir
    Apr 28, 2012 at 11:16
  • 1
    The whole intention was to disable the permalinks options page, so maybe I can get away with it? Apr 28, 2012 at 15:08
  • 3
    It should be called on 'after_switch_theme' or on plugin activation and be followed by 'flush_rewrite_rules()'
    – csstd
    Oct 27, 2013 at 4:27

Previous Answer is not working. I have gotten a pure solution. Can use Use this code. It will work 100%. Thanks

 * Rewrite set up, when theme activate i mean
if (isset($_GET['activated']) && is_admin()) {
    global $wp_rewrite;

* Redirect to Permalink setting Page.
* Otherwise Redirect rule will not work Properly.
function redirect_to_permalink() {

add_action( 'after_switch_theme', 'redirect_to_permalink' );
function setPermaLink(){
    $ps = '/%category%/%postname%/';
    $permalink_structure = sanitize_option( 'permalink_structure', $ps);
    $blog_prefix = '/blog';
    $prefix = '/index.php';

    if ( ! empty( $permalink_structure ) ) {
        $permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
        if ( $prefix && $blog_prefix ) {
            $permalink_structure = $prefix . preg_replace( '#^/?index\.php#', '', $permalink_structure );
        } else {
            $permalink_structure = $blog_prefix . $permalink_structure;

    $wp_rewrite->set_permalink_structure( $permalink_structure );


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.