save_mod_rewrite_rules

函数
save_mod_rewrite_rules ( No parameters )
返回值
  • (bool|null) True on write success, false on failure. Null in multisite.
定义位置
相关方法
add_rewrite_ruleiis7_save_url_rewrite_rulesiis7_add_rewrite_ruleflush_rewrite_rulesiis7_delete_rewrite_rule
引入
1.5.0
弃用
-

save_mod_rewrite_rules函数用来保存mod_rewrite规则到你的WordPress安装根目录下的.htaccess文件。mod_rewrite规则是用来使WordPress为你的文章和页面创建搜索引擎友好的URL的。

如果htaccess文件是可写的,则用当前规则更新该文件。

如果该文件存在并可写入,则始终写入该文件,以确保我们清空旧规则。

function save_mod_rewrite_rules() {
	global $wp_rewrite;

	if ( is_multisite() ) {
		return;
	}

	// Ensure get_home_path() is declared.
	require_once ABSPATH . 'wp-admin/includes/file.php';

	$home_path     = get_home_path();
	$htaccess_file = $home_path . '.htaccess';

	/*
	 * If the file doesn't already exist check for write access to the directory
	 * and whether we have some rules. Else check for write access to the file.
	 */
	if ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks()
		|| is_writable( $htaccess_file )
	) {
		if ( got_mod_rewrite() ) {
			$rules = explode( "n", $wp_rewrite->mod_rewrite_rules() );

			return insert_with_markers( $htaccess_file, 'WordPress', $rules );
		}
	}

	return false;
}

常见问题

FAQs
查看更多 >