wppaste
WordPress

iis7_add_rewrite_rule( string $filename, string $rewrite_rule ): bool

Since
2.8.0
Source
wp-admin/includes/misc.php:894
Adds WordPress rewrite rule to the IIS 7+ configuration file.

Compatibility

WordPress
since 2.8.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.

Parameters

$filenamestring
The file path to the configuration file.
$rewrite_rulestring
The XML fragment with URL Rewrite rule.

Return value

bool

Performance profile

How much work a call to iis7_add_rewrite_rule() 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–115

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • filesystemfilesystem accessfile_exists()called directly
  • sqldatabase query->query()called directly

What one call costs · 15 distinct outcomes

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

WhenInstructionsCalls it makes
always5none
file_exists()19file_exists(), ->load()
file_exists()30file_exists(), ->load(), ->query()
!file_exists()31file_exists(), fopen(), fwrite(), fclose(), ->load()
!file_exists()42file_exists(), fopen(), fwrite(), fclose(), ->load(), ->query()
file_exists()59file_exists(), ->load(), ->query(), ->query(), ->item(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
!file_exists()71file_exists(), fopen(), fwrite(), fclose(), ->load(), ->query(), ->query(), ->item(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
file_exists()73file_exists(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->item(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
!file_exists()85file_exists(), fopen(), fwrite(), fclose(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->item(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
file_exists()87file_exists(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->createElement(), ->appendChild(), ->query(), ->item(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
!file_exists()99file_exists(), fopen(), fwrite(), fclose(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->createElement(), ->appendChild(), ->query(), ->item(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
file_exists()101file_exists(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->createElement(), ->appendChild(), ->query(), ->createElement(), ->appendChild(), ->query(), ->item(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
3 further outcomes, up to 115 instructions
file_exists()103file_exists(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->createElement(), ->appendChild(), ->query(), ->createElement(), ->appendChild(), ->query(), ->createElement(), ->appendChild(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
!file_exists()113file_exists(), fopen(), fwrite(), fclose(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->createElement(), ->appendChild(), ->query(), ->createElement(), ->appendChild(), ->query(), ->item(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()
!file_exists()115file_exists(), fopen(), fwrite(), fclose(), ->load(), ->query(), ->query(), ->createElement(), ->query(), ->createElement(), ->appendChild(), ->query(), ->createElement(), ->appendChild(), ->query(), ->createElement(), ->appendChild(), ->appendChild(), ->createDocumentFragment(), ->appendXML(), ->appendChild(), saveDomDocument()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1475–1158
8.51475–1158
8.41475–11583 fewer instructions than PHP 8.3
8.31508–1188
8.21508–1188
8.11508–1188
7.41508–1188

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 · 3

  • saveDomDocument()Saves the XML document into a file.
  • DOMDocument::__construct()
  • DOMXPath::__construct()

Used by · 1

Source code

function iis7_add_rewrite_rule( $filename, $rewrite_rule ) {	if ( ! class_exists( 'DOMDocument', false ) ) {		return false;	} 	// If configuration file does not exist then we create one.	if ( ! file_exists( $filename ) ) {		$fp = fopen( $filename, 'w' );		fwrite( $fp, '<configuration/>' );		fclose( $fp );	} 	$doc                     = new DOMDocument();	$doc->preserveWhiteSpace = false; 	if ( $doc->load( $filename ) === false ) {		return false;	} 	$xpath = new DOMXPath( $doc ); 	// First check if the rule already exists as in that case there is no need to re-add it.	$wordpress_rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' ); 	if ( $wordpress_rules->length > 0 ) {		return true;	} 	// Check the XPath to the rewrite rule and create XML nodes if they do not exist.	$xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite/rules' ); 	if ( $xml_nodes->length > 0 ) {		$rules_node = $xml_nodes->item( 0 );	} else {		$rules_node = $doc->createElement( 'rules' ); 		$xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite' ); 		if ( $xml_nodes->length > 0 ) {			$rewrite_node = $xml_nodes->item( 0 );			$rewrite_node->appendChild( $rules_node );		} else {			$rewrite_node = $doc->createElement( 'rewrite' );			$rewrite_node->appendChild( $rules_node ); 			$xml_nodes = $xpath->query( '/configuration/system.webServer' ); 			if ( $xml_nodes->length > 0 ) {				$system_web_server_node = $xml_nodes->item( 0 );				$system_web_server_node->appendChild( $rewrite_node );			} else {				$system_web_server_node = $doc->createElement( 'system.webServer' );				$system_web_server_node->appendChild( $rewrite_node ); 				$xml_nodes = $xpath->query( '/configuration' ); 				if ( $xml_nodes->length > 0 ) {					$config_node = $xml_nodes->item( 0 );					$config_node->appendChild( $system_web_server_node );				} else {					$config_node = $doc->createElement( 'configuration' );					$doc->appendChild( $config_node );					$config_node->appendChild( $system_web_server_node );				}			}		}	} 	$rule_fragment = $doc->createDocumentFragment();	$rule_fragment->appendXML( $rewrite_rule );	$rules_node->appendChild( $rule_fragment ); 	$doc->encoding     = 'UTF-8';	$doc->formatOutput = true;	saveDomDocument( $doc, $filename ); 	return true;}

Changelog

Introduced in 2.8.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.