1

I need my theme to default Permalink Type to Postname. Upon activation, my theme is executing the following code:

   //change permalinks to /%postname%/
    global $wp_rewrite; 

    //Write the rule
    $wp_rewrite->set_permalink_structure('/%postname%/'); 

    //Flush the rules and tell it to write htaccess or IIS
    $wp_rewrite->flush_rules();

As part of the process of activating permalinks, WordPress updates web server rewrite rules. This works fine on a *nix web server, updating the rewrite rules in .htaccess, but it doesn’t create the rewrite rules for IIS.

Ok, maybe to do with permissions on the Windows machine?

Here’s the anomaly, on Windows, when the permalink is updated from the permalink settings page, the rewrite rules are correctly set in the IIS web.config.

MY QUESTION IS, with Windows IIS, how come the settings page updates the permalink type without a problem, yet the above code doesn’t (but does on *nix)?

I will NOT write a script to manually update the rewrite rules. It should be upto the WordPress API to do this.

1 Answer 1

-1

I don't think it's a good approach. Themes shouldn't change any settings and definitely not settings like permalink structure.

If I've set my permalinks structure to given one, then I had my reasons to do it. Such change have many effects. If you change the permalink structure you cause many SEO problems. But also - the site may stop working at all (for example if the server doesn't support permalinks at all or if it doesn't have access rights to .htaccess file).

So... The real question is not how to force WP to recalculate the permalinks, but who to avoid such operation at all.

If you really need a permalink structure to be set to given one, then you can politely ask user to change it using admin notice.

But to be honest, I really doubt that a theme should require such changes. It should work with any permalink structure.

4
  • I appreciate you giving an idea about best practice, but that's not what I asked for. The WordPress backend is purely being used as a configuration mechanism for a "single page" web based application for a single customer. They will have no need to use any other theme, but they may have to reinstall WordPress and this theme at any point. My aim, to make the experience for the customer as simple as possible, is to automate as many set up tasks as possible. This is just one of them.
    – dewd
    Mar 28, 2019 at 21:12
  • And yet this is a Q&A site and every question is meant to be applicable for later usage. In this case, without full context, this is the best answer I can provide - theme should not do such changes. And if you have a very peculiar case - then it is off topic as "too localized", I'm afraid. Mar 28, 2019 at 21:16
  • I respect your answer and your response in this thread. I'm not sure this is too localised though as WordPress is often being used as a SPA especially with the now built in wpjson API. I'm yet to hear a reason why a manual process shouldn't use an available WordPress API to automate it. It makes no difference to other themes as users of those can set permalinks as they wish.
    – dewd
    Mar 29, 2019 at 22:30
  • 1
    @dewd Because theme should be responsible only for displaying the content. It should not change any settings. It’s like you’ve install a new wallpaper on your phone and it would switch some settings. It won’t be intuitive and in a year or so you will forget about it and it will be a horror to maintain. Themes are for displaying content, plugins are for delivering functions and settings are for settings. And it doesn’t matter what you do and what you code, using these things for other purposes will always end bad... Mar 30, 2019 at 6:24

Your Answer

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

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