1

I have a subdomain multisite network. I have implemented a system in order to create new subdomains programmatically, just filling a form.

Now I have a need: when a new blog is created, I'd like to set its permalink structure to /postname/.

I've tried these solutions:

How to set permalink structure via functions.php

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

I've added this snippet in the functions.php of the theme used for the main site, where the system above mentioned is. It simply doesn't work.

How Do I Programmatically Force Custom Permalinks with My Theme?

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

I have some concerns with this snippet: I don't think it could be a good idea to fire the rewrite on every "init" (also someone else expressed doubt about this in comments); in second place, this snippet has to be placed on the default theme of new blogs - but what if I want to change the default theme in future?

Thank you very much for every suggestions,

Marco

3

With a "mu-plugins", the solution is...

add_action( 'wpmu_new_blog', function( $blog_id ){

switch_to_blog( $blog_id );
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%postname%/');
$wp_rewrite->flush_rules();
restore_current_blog();

}, 10 );
1

You should be able to create an mu-plugin (required plugin) that fires on the activate_blog hook. Something like this should get you started in the right direction:

<?php
/* Plugin name: WPSE Permalink Setter
Description: Sets permalink structure for each blog activation
*/
add_action('activate_blog', 'wpse_271662_set_permalinks');
function wpse_271662_set_permalinks() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}
?>

Since the code only runs when a new site is created (activate_blog), this prevents performance issues associated with running on every init, and it doesn't rely on a theme. Of course in theory, site admins could change the permalink structure later, but this would give them a clean install with the permalink structure you want, unless they monkey around with it.

Place this file directly inside /wp-content/mu-plugins/ (no subfolder like normal plugins) so it will run automatically unless the file is deleted.

  • Hi WebElaine, thank you for your answer! One question: how am I sure about the fact that I'm not changing the permalink structure of the main blog (where the new blog creation system works)? – Marco Panichi Jun 29 '17 at 9:04
  • I've did some tries, unfortunally it doesn't work. The plugin shows up correctly under the mu plugins in network dashboard, but when a new blog is created the permalink structure remains the same – Marco Panichi Jun 29 '17 at 10:10
  • You may have to modify the code to set the blog ID. It looks like your original code was probably only good for single installs, whereas you're trying to affect the particular MultiSite subsite that just got activated. – WebElaine Jun 29 '17 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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