Search code examples
phpwordpresspermalinksmultisite

WP sub-domains multisite: How to change default permalink structure for new sites?


I have a WP multisite set up with sub-domains for all sites.

I would like the URL structure for each new site to be:

subdomain.maindomain.com/post-name

For instance: subdomain.maindomain.com/hi-world

I've been struggling with this for a day and a half, and I hope somebody here can help me. I have read a LOT of info, and tried with editing the default theme's functions.php and also adding a custom plugin in mu-plugins. But I have not succeeded yet.

So the regular setting for permalinks is now: /%year%/%monthnum%/%day%/%postname%/

But I'd like to have: /%postname%/

Ok, I have tried many different things. This is probably the closest to success (?):

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

In the functions.php I have this for adding standard pages to the new blogs/ sites automatically:

add_action('wpmu_new_blog', 'wpb_create_my_pages', 10, 2);

// create new page
  $page_id = wp_insert_post(array(
    'post_title'     => 'My title',
    'post_name'      => 'The name',
    'post_content'   => 'The content here',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 10,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
 ));  

So in this I have tried the 'set_permalink' function, but it doesn't work.

I have also tried making my own mu-plugins plugin, but haven't got that working neither.

When I Google I keep finding solutions that require the new blog's owner to log in and save the permalink structure, but I simply want the permalink structure to be the same for all new blogs/ sites.

How can I set the default permalink structure for new sites?

Thanks for any pointers or code that can help me with this!


Solution

  • Just for future proofing this question with an answer, and for reference for others:

    1. I created a new sub directory in /wp-content/ named "mu-plugins", so I had /wp-content/mu-plugins/

    2. In "mu-plugins" I created a file named "rewritepermalinks.php"

    In it I put:

    <?php
    
    /**
     * Plugin Name: Permalin Structure Edit
     * Description: Edit the permalinks structure on new multisite sites
     * Author:      
     * License:     GNU General Public License v3 or later
     * License URI: http://www.gnu.org/licenses/gpl-3.0.html
     */
    
    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 );
    ?>
    

    This then worked for me on Wordpress 5.8.3 and with multisite enabled. The solution was found here and also mentioned in the comments for the question. I had tried it once before but must have had a typo because it didn't work the first time.

    I have tried to use 'wp_insert_site' in stead, but haven't got that to work. There is a hint in the comments here about wp_insert_site that might be of value to get this working.

    For now the above code works for me as a MU plugin.