views:

39

answers:

1

I've been having issues on creating new wordpress blogs where I'm setting permalinks via script on theme activation. However, even though they appear to be correct when I check the permalink settings in WP, my new pages are throwing 404 errors.

The only fix I've found is that I have to go back to permalink options and click "Save Changes", even though, according to the display, I've made no changes to need to save...

I'm setting permalinks to /%postname%/

Here's how I'm doing it.

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

That script gets run when my theme is activated.

Any ideas why it only partially does the job?

+1  A: 

When you set the permalink structure in the Wordpress settings panel it calls the set_permalink_structure function in wp_includes/rewrite.php. In addition to setting the permalink_structure option this function also reinitialises the WordPress rewrite component and runs the permalink_structure_changed action.

You should call this function instead of setting the option directly, something like this should work:

if (get_option('permalink_structure') == '') {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
}
Richard M