wppaste
WordPress

insert_with_markers( string $filename, string $marker, array|string $insertion ): bool

Since
1.5.0
Source
wp-admin/includes/misc.php:114
Inserts an array of strings into a file (.htaccess), placing it between BEGIN and END markers.

Description

Replaces existing marked info. Retains surrounding data. Creates file if none exists.

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.

Parameters

$filenamestring
Filename to alter.
$markerstring
The marker to alter.
$insertionarray|string
The new content to insert.

Return value

bool
True on write success, false on failure.

Performance profile

How much work a call to insert_with_markers() 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
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–135

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

Plugin surface
1 hook

Third-party callbacks on 'insert_with_markers_inline_instructions' run inside this call, and their cost is not bounded by anything here.

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_exists()called directly
  • optionnetwork optionget_site_option()one call below insert_with_markers()

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 · 50 distinct outcomes

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

WhenInstructionsCalls it makes
!is_writable()12–13file_exists(), is_writable()
!file_exists() && is_writable() && !touch()17file_exists(), is_writable(), touch()
file_exists() && is_writable() && is_array($insertion)55–56file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen()
file_exists() && is_writable() && is_array($insertion)57–58file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen()
file_exists() && is_writable() && !is_array($insertion)60–61file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen()
file_exists() && is_writable() && !is_array($insertion)62–63file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion)64–65file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion)66–67file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion)69–70file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion)70–71file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion)71–72file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion)72–73file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen()
38 further outcomes, up to 135 instructions
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion)75–76file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen()
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion)77–78file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen()
file_exists() && is_writable() && is_array($insertion) && feof() && $existing_lines === false81–83file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
file_exists() && is_writable() && is_array($insertion) && feof() && $existing_lines === false83–85file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
file_exists() && is_writable() && !is_array($insertion) && feof() && $existing_lines === false86–88file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
file_exists() && is_writable() && !is_array($insertion) && feof() && $existing_lines === false88–90file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion) && feof() && $existing_lines === false90–92file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion) && feof() && $existing_lines === false92–94file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion) && feof() && $existing_lines === false95–97file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion) && feof() && $existing_lines === false96–98file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion) && feof() && $existing_lines === false97–99file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion) && feof() && $existing_lines === false98–100file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion) && feof() && $existing_lines === false101–103file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion) && feof() && $existing_lines === false103–105file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), flock(), fclose()
file_exists() && is_writable() && is_array($insertion) && feof() && $existing_lines !== false && !fwrite()104–106file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
file_exists() && is_writable() && is_array($insertion) && feof() && $existing_lines !== false && !fwrite()106–108file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
file_exists() && is_writable() && !is_array($insertion) && feof() && $existing_lines !== false && !fwrite()109–111file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
file_exists() && is_writable() && is_array($insertion) && feof() && $existing_lines !== false && fwrite()111–113file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
file_exists() && is_writable() && !is_array($insertion) && feof() && $existing_lines !== false && !fwrite()111–113file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion) && feof() && $existing_lines !== false && !fwrite()113–115file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
file_exists() && is_writable() && is_array($insertion) && feof() && $existing_lines !== false && fwrite()113–115file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion) && feof() && $existing_lines !== false && !fwrite()115–117file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
file_exists() && is_writable() && !is_array($insertion) && feof() && $existing_lines !== false && fwrite()116–118file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && !fwrite()118–120file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
file_exists() && is_writable() && !is_array($insertion) && feof() && $existing_lines !== false && fwrite()118–120file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion) && feof() && $existing_lines !== false && !fwrite()119–121file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion) && feof() && $existing_lines !== false && fwrite()120–122file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && !fwrite()120–122file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion) && feof() && $existing_lines !== false && !fwrite()121–123file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && is_array($insertion) && feof() && $existing_lines !== false && fwrite()122–124file_exists(), is_writable(), touch(), fileperms(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && !fwrite()124–126file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && fwrite()125–127file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion) && feof() && $existing_lines !== false && fwrite()126–128file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && !fwrite()126–128file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && !fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && fwrite()127–129file_exists(), is_writable(), touch(), fileperms(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && is_array($insertion) && feof() && $existing_lines !== false && fwrite()128–130file_exists(), is_writable(), touch(), fileperms(), chmod(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && fwrite()131–133file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()
!file_exists() && is_writable() && touch() && fileperms() && !is_array($insertion) && feof() && $existing_lines !== false && fwrite()133–135file_exists(), is_writable(), touch(), fileperms(), chmod(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), restore_previous_locale(), array_merge(), fopen(), flock(), feof(), fseek(), fwrite(), ftell(), ftruncate(), fflush(), flock(), fclose()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev18512–13521
8.518512–13521
8.418512–1352112 fewer instructions than PHP 8.3
8.319712–14121
8.219712–14121
8.119712–14121
7.419712–14121

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.

Hooks and filters fired · 1

One hook fires while insert_with_markers() runs, in this order:

  1. apply_filters( insert_with_markers_inline_instructions )filterline 164 (+50 into the body)

    Filters the inline instructions inserted before the dynamically generated content.

Uses · 6

Used by · 1

Source code

function insert_with_markers( $filename, $marker, $insertion ) {	if ( ! file_exists( $filename ) ) {		if ( ! is_writable( dirname( $filename ) ) ) {			return false;		} 		if ( ! touch( $filename ) ) {			return false;		} 		// Make sure the file is created with a minimum set of permissions.		$perms = fileperms( $filename ); 		if ( $perms ) {			chmod( $filename, $perms | 0644 );		}	} elseif ( ! is_writable( $filename ) ) {		return false;	} 	if ( ! is_array( $insertion ) ) {		$insertion = explode( "\n", $insertion );	} 	$switched_locale = switch_to_locale( get_locale() ); 	$instructions = sprintf(		/* translators: 1: Marker. */		__(			'The directives (lines) between "BEGIN %1$s" and "END %1$s" aredynamically generated, and should only be modified via WordPress filters.Any changes to the directives between these markers will be overwritten.'		),		$marker	); 	$instructions = explode( "\n", $instructions ); 	foreach ( $instructions as $line => $text ) {		$instructions[ $line ] = '# ' . $text;	} 	/**	 * Filters the inline instructions inserted before the dynamically generated content.	 *	 * @since 5.3.0	 *	 * @param string[] $instructions Array of lines with inline instructions.	 * @param string   $marker       The marker being inserted.	 */	$instructions = apply_filters( 'insert_with_markers_inline_instructions', $instructions, $marker ); 	if ( $switched_locale ) {		restore_previous_locale();	} 	$insertion = array_merge( $instructions, $insertion ); 	$start_marker = "# BEGIN {$marker}";	$end_marker   = "# END {$marker}"; 	$fp = fopen( $filename, 'r+' ); 	if ( ! $fp ) {		return false;	} 	// Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired.	flock( $fp, LOCK_EX ); 	$lines = array(); 	while ( ! feof( $fp ) ) {		$lines[] = rtrim( fgets( $fp ), "\r\n" );	} 	// Split out the existing file into the preceding lines, and those that appear after the marker.	$pre_lines        = array();	$post_lines       = array();	$existing_lines   = array();

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 7.1.0 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.