如何在wordpress中动态编写.htaccess?

时间:2013-10-12 10:58:03

标签: .htaccess mod-rewrite wordpress-theming wordpress

我试图在主题激活上设置永久链接结构。以下函数包含在theme.php的主题中。

// Executes function on theme activation
function myactivationfunction() {

    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/%category%/%postname%/' );
    // register taxonomies/post types here
    flush_rewrite_rules();

}
add_action("after_switch_theme", "myactivationfunction", 10 ,  2);

此代码正常工作,但值不写入.htaccess或.htaccess而不是创建。如何动态编写.htaccess文件?

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:1)

flush必须在admin_init上运行,你在主题开关回调中调用它,所以它只运行一次:

add_action( 'after_switch_theme', 'activate_so_19333403' );

function activate_so_19333403() {
    add_action( 'admin_init', 'flush_rewrites_so_19333403' );
}

function flush_rewrites_so_19333403() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/%category%/%postname%/' );
    flush_rewrite_rules();  
}