insert_with_markers( string $filename, string $marker, array|string $insertion ): bool
- Since
- 1.5.0
- Source
wp-admin/includes/misc.php:114
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
- Scaling
- Scales with input
- Instructions
- 12–135
- Plugin surface
- 1 hook
- Called by
- 1
Reads or writes the filesystem via file_exists().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 185.
Third-party callbacks on 'insert_with_markers_inline_instructions' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_exists()called directly - optionnetwork option
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_writable() | 12–13 | file_exists(), is_writable() |
!file_exists() && is_writable() && !touch() | 17 | file_exists(), is_writable(), touch() |
file_exists() && is_writable() && is_array($insertion) | 55–56 | file_exists(), is_writable(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen() |
file_exists() && is_writable() && is_array($insertion) | 57–58 | file_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–61 | file_exists(), is_writable(), explode(), get_locale(), switch_to_locale(), __(), sprintf(), explode(), apply_filters(), array_merge(), fopen() |
file_exists() && is_writable() && !is_array($insertion) | 62–63 | file_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–65 | file_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–67 | file_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–70 | file_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–71 | file_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–72 | file_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–73 | file_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–76 | file_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–78 | file_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 === false | 81–83 | file_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 === false | 83–85 | file_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 === false | 86–88 | file_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 === false | 88–90 | file_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 === false | 90–92 | file_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 === false | 92–94 | file_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 === false | 95–97 | file_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 === false | 96–98 | file_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 === false | 97–99 | file_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 === false | 98–100 | file_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 === false | 101–103 | file_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 === false | 103–105 | file_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–106 | file_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–108 | file_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–111 | file_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–113 | file_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–113 | file_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–115 | file_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–115 | file_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–117 | file_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–118 | file_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–120 | file_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–120 | file_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–121 | file_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–122 | file_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–122 | file_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–123 | file_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–124 | file_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–126 | file_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–127 | file_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–128 | file_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–128 | file_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–129 | file_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–130 | file_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–133 | file_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–135 | file_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 185 | 12–135 | 21 | |
| 8.5 | 185 | 12–135 | 21 | |
| 8.4 | 185 | 12–135 | 21 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 197 | 12–141 | 21 | |
| 8.2 | 197 | 12–141 | 21 | |
| 8.1 | 197 | 12–141 | 21 | |
| 7.4 | 197 | 12–141 | 21 |
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:
- 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
- switch_to_locale()Switches the translations according to the given locale.
- get_locale()Retrieves the current locale.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- restore_previous_locale()Restores the translations according to the previous locale.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Used by · 1
- save_mod_rewrite_rules()Updates the htaccess file with the current rules if it is writable.
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.
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.