save_mod_rewrite_rules()WP 1.5.0

The function updates the .htaccess file if it is writable. It removes old rules and adds new ones.

The function is triggered on each call of the function flush_rewrite_rules() when the first parameter = true (this is the default). Therefore, it usually does not need to be called separately.

Does not work for multisite - the check is performed by the function is_multisite().

Will only work on an Apache server with the mod_rewrite module loaded - the check is performed by the function got_mod_rewrite().

No Hooks.

Returns

true|false|null. Nothing (null).

Usage

save_mod_rewrite_rules();

Examples

1

#1 Disable the .htaccess update

By default when flush_rewrite_rules() is called, save_mod_rewrite_rules() is triggered. If we don't need this behavior, we can disable it with flush_rewrite_rules_hard hook:

add_filter( 'flush_rewrite_rules_hard', '__return_false' );

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 1.5.0 Introduced.

save_mod_rewrite_rules() code WP 7.0

function save_mod_rewrite_rules() {
	global $wp_rewrite;

	if ( is_multisite() ) {
		return null;
	}

	// 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;
}