wppaste
WordPress

save_mod_rewrite_rules(): bool|null

Since
1.5.0
Source
wp-admin/includes/misc.php:262
Updates the htaccess file with the current rules if it is writable.

Description

Always writes to the file if it exists and is writable to ensure that we blank out old rules.

Compatibility

WordPress
since 1.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

bool|null
True on write success, false on failure. Null in multisite.

Performance profile

How much work a call to save_mod_rewrite_rules() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_exists().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–43

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 45.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfile_exists()called directly
  • optionoption read or writeget_option()one call below save_mod_rewrite_rules()
  • hookthird-party callbacksapply_filters()one call below save_mod_rewrite_rules()

Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost save_mod_rewrite_rules() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_multisite()5is_multisite()
!is_multisite() && file_exists() && !is_writable()21is_multisite(), get_home_path(), file_exists(), is_writable()
!is_multisite() && file_exists() && is_writable() && !got_mod_rewrite()24is_multisite(), get_home_path(), file_exists(), is_writable(), got_mod_rewrite()
!is_multisite() && !file_exists() && !is_writable()25is_multisite(), get_home_path(), file_exists(), is_writable(), is_writable()
!is_multisite() && !file_exists() && is_writable() && ->using_mod_rewrite_permalinks() && !got_mod_rewrite()27is_multisite(), get_home_path(), file_exists(), is_writable(), ->using_mod_rewrite_permalinks(), got_mod_rewrite()
!is_multisite() && !file_exists() && !got_mod_rewrite()28is_multisite(), get_home_path(), file_exists(), is_writable(), is_writable(), got_mod_rewrite()
!is_multisite() && !file_exists() && !->using_mod_rewrite_permalinks()28is_multisite(), get_home_path(), file_exists(), is_writable(), ->using_mod_rewrite_permalinks(), is_writable()
!is_multisite() && !file_exists() && is_writable() && !->using_mod_rewrite_permalinks() && !got_mod_rewrite()31is_multisite(), get_home_path(), file_exists(), is_writable(), ->using_mod_rewrite_permalinks(), is_writable(), got_mod_rewrite()
!is_multisite() && file_exists() && is_writable() && got_mod_rewrite()36is_multisite(), get_home_path(), file_exists(), is_writable(), got_mod_rewrite(), ->mod_rewrite_rules(), explode(), insert_with_markers()
!is_multisite() && !file_exists() && is_writable() && ->using_mod_rewrite_permalinks() && got_mod_rewrite()39is_multisite(), get_home_path(), file_exists(), is_writable(), ->using_mod_rewrite_permalinks(), got_mod_rewrite(), ->mod_rewrite_rules(), explode(), insert_with_markers()
!is_multisite() && !file_exists() && got_mod_rewrite()40is_multisite(), get_home_path(), file_exists(), is_writable(), is_writable(), got_mod_rewrite(), ->mod_rewrite_rules(), explode(), insert_with_markers()
!is_multisite() && !file_exists() && is_writable() && !->using_mod_rewrite_permalinks() && got_mod_rewrite()43is_multisite(), get_home_path(), file_exists(), is_writable(), ->using_mod_rewrite_permalinks(), is_writable(), got_mod_rewrite(), ->mod_rewrite_rules(), explode(), insert_with_markers()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 45 instructions, 5–43 executed per call, 6 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

  • is_multisite()Determines whether Multisite is enabled.
  • get_home_path()Gets the absolute filesystem path to the root of the WordPress installation.
  • got_mod_rewrite()Returns whether the server is running Apache with the mod_rewrite module loaded.
  • insert_with_markers()Inserts an array of strings into a file (.htaccess), placing it between BEGIN and END markers.

Used by · 2

Source code

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

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-admin/includes/misc.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.