English 中文(简体)
WordPress > setting permalink option via script buggy?
原标题:

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?

最佳回答

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();
}
问题回答

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?





相关问题
Wordpress hiding Post Div

I wanted to know how to make it possible that if there is no post i would like to be able to hide the DIV that controls the latest blog post only having them appear when someone post somethign on a ...

Echoing a string that contains PHP (in WordPress)

I have a custom field in WordPress called "thumb-url" which contains the exact location of an image. I want to only display the image if "thumb-url" contains the location of the image. I m start with ...

Display author URL in single.php

How can I display the author website in single.php template, only if the author has entered a URL in her/his profile. I m displaying the URL in author.php but the same code doesn t work in other ...

Looping through wordpress categories

I am new to Wordpress and been pulling my hair out trying to create a category loop. The loop is supposed to: loop through all categories echo out the category name (with link to echo out the last ...

How do I concatenate a string in a PHP Array Element?

I am customizing wordpress blog and I have a need to make custom sidebar widgets. My PHP is rusty at best. What I am trying to do is concatenate a php variable into a string being set as an array ...

Wordpress Navigation

I am working on a Wordpress Theme, I need to work on the navigation, I am having a little trouble creating it. The navigation I am looking for looks like this: www.neu.edu/humanities. I have gotten ...

WordPress > setting permalink option via script buggy?

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 ...