WordPress >通过脚本错误设置永久链接选项? [英] WordPress > setting permalink option via script buggy?

查看:24
本文介绍了WordPress >通过脚本错误设置永久链接选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主题的自定义选项面板有以下代码...

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

`/* 初始化站点选项 */

` /* initialize the site options */

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

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

这会检查永久链接选项设置,并且因为 WP 默认值是 "",这会触发 site.com/?p=x 处理程序.这样,如果用户还没有设置默认的永久链接,我的脚本会通过将永久链接设置为帖子名称来为他们设置.或者至少我是这么想的...

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

但是,有一些拥有我的模板的人告诉我,在第一次安装时,他们在页面上遇到了 404 错误.

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.

有人知道为什么会这样吗?除了在上面的代码中调用 update_options() 时会发生什么之外,它们可能是数据库中确定永久链接的另一个设置吗?

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?

推荐答案

嗯,这可能是因为您正在更新数据库表 (permalink_structure) 中的值,而 .htaccess 保持不变,这就是 mod_rewrite 未加载的原因并且用户在页面上收到 404 错误.

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.

我相信 WordPress 还会将重写规则添加到 .htaccess 中,以便在您单击管理面板中的保存更改"时启用永久链接.让我挖掘一下,看看 WP 到底在做什么.

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.

编辑.

好的,这是正在执行您要完成的操作的代码:

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();
}

这篇关于WordPress &gt;通过脚本错误设置永久链接选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆