Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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' );
share|improve this answer
    
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... –  tbuteler Oct 16 '11 at 21:26
    
I assume you're running PHP < 5.3, where closures (anonymous functions) were not available, so I've edited my answer to include the old school way. I tested both code in my theme's functions.php, so let me know if this works.Another thing I noticed is that I typed %year& instead of %year%, so fixed that too. –  soulseekah Oct 17 '11 at 2:01
    
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 '11 at 4:41
    
I am running 5.2.17, so that explains it! Second version works out of the box :) Thank you! –  tbuteler Oct 17 '11 at 23:21
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 '12 at 11:16
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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