views:

155

answers:

2

My theme's custom options panel has the following code...

` /* initialize the site options */

if(get_option('permalink_structure')==""){update_option('permalink_structure', '/%postname%/');} `

This checks the permalink option setting and since the WP default is "" which triggers the site.com/?p=x handler. This way, if the user has not yet set permalinks from the default, my script does it for them, by setting permalink to post name. Or at least that what I thought...

However, I've had a few folks who have my template tell me that upon first install, they were getting 404 errors on pages.

Apparently, the workaround is to physically navigate to the Permalinks page and just click "Save Changes" (even though when you first hit this page, the Permalink comes up as if it's correctly entered into the "custom" field.

Anyone know why this happens? Is their perhaps another setting in the db that determines the permalink in addition to what happens when update_options() is called as in the above code?

A: 

wp_rewrite does not appear to have any effect. Users still have to manually click "Save Options" on the permalinks screen.

I suppose I will run firebug on that page during the update to see what's getting set that update_options is apparently missing.

This would appear to be a bug in update_options when the option being updated is permalink_structure.

Anyone disagree?

Scott B
A: 

Well, this probably happens because you're updating value in database table (permalink_structure), while .htaccess remains the same, and that's why mod_rewrite isn't loaded and users are getting 404-errors on pages.

I believe WordPress also adds rewriting rules into .htaccess in order to enable permalinks when you're clicking "Save Changes" in admin panel. Let me dig it out and find out what WP is doing exactly.


EDIT.

Ok, here is the code that is doing what you're trying to accomplish:

<?php

if (get_option('permalink_structure') == "")
{
    // Including files responsible for .htaccess update
    require_once(ABSPATH . 'wp-admin/includes/misc.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');

    // Prepare WordPress Rewrite object in case it hasn't been initialized yet
    if (empty($wp_rewrite) || !($wp_rewrite instanceof WP_Rewrite))
    {
     $wp_rewrite = new WP_Rewrite();
    }

    // Update permalink structure
    $permalink_structure = '/%postname%/';
    $wp_rewrite->set_permalink_structure($permalink_structure);

    // Recreate rewrite rules
    $wp_rewrite->flush_rules();
}
Oleksandr Bernatskyi