Title: insert_with_markers
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#wp--skip-link--target)

Inserts an array of strings into a file (.htaccess), placing it between BEGIN and
END markers.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#description)󠁿

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#parameters)󠁿

 `$filename`stringrequired

Filename to alter.

`$marker`stringrequired

The marker to alter.

`$insertion`array|stringrequired

The new content to insert.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#return)󠁿

 bool True on write success, false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#source)󠁿

    ```php
    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" are
    dynamically 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();
    	$found_marker     = false;
    	$found_end_marker = false;

    	foreach ( $lines as $line ) {
    		if ( ! $found_marker && str_contains( $line, $start_marker ) ) {
    			$found_marker = true;
    			continue;
    		} elseif ( ! $found_end_marker && str_contains( $line, $end_marker ) ) {
    			$found_end_marker = true;
    			continue;
    		}

    		if ( ! $found_marker ) {
    			$pre_lines[] = $line;
    		} elseif ( $found_end_marker ) {
    			$post_lines[] = $line;
    		} else {
    			$existing_lines[] = $line;
    		}
    	}

    	// Check to see if there was a change.
    	if ( $existing_lines === $insertion ) {
    		flock( $fp, LOCK_UN );
    		fclose( $fp );

    		return true;
    	}

    	// Generate the new file data.
    	$new_file_data = implode(
    		"\n",
    		array_merge(
    			$pre_lines,
    			array( $start_marker ),
    			$insertion,
    			array( $end_marker ),
    			$post_lines
    		)
    	);

    	// Write to the start of the file, and truncate it to that length.
    	fseek( $fp, 0 );
    	$bytes = fwrite( $fp, $new_file_data );

    	if ( $bytes ) {
    		ftruncate( $fp, ftell( $fp ) );
    	}

    	fflush( $fp );
    	flock( $fp, LOCK_UN );
    	fclose( $fp );

    	return (bool) $bytes;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/misc.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-admin/includes/misc.php#L114)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-admin/includes/misc.php#L114-L248)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#hooks)󠁿

 [apply_filters( ‘insert_with_markers_inline_instructions’, string[] $instructions, string $marker )](https://developer.wordpress.org/reference/hooks/insert_with_markers_inline_instructions/)

Filters the inline instructions inserted before the dynamically generated content.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#related)󠁿

| Uses | Description | 
| [restore_previous_locale()](https://developer.wordpress.org/reference/functions/restore_previous_locale/)`wp-includes/l10n.php` |

Restores the translations according to the previous locale.

  | 
| [switch_to_locale()](https://developer.wordpress.org/reference/functions/switch_to_locale/)`wp-includes/l10n.php` |

Switches the translations according to the given locale.

  | 
| [get_locale()](https://developer.wordpress.org/reference/functions/get_locale/)`wp-includes/l10n.php` |

Retrieves the current locale.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#)

| Used by | Description | 
| [save_mod_rewrite_rules()](https://developer.wordpress.org/reference/functions/save_mod_rewrite_rules/)`wp-admin/includes/misc.php` |

Updates the htaccess file with the current rules if it is writable.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 2 content](https://developer.wordpress.org/reference/functions/insert_with_markers/?output_format=md#comment-content-5740)
 2.    [zeno001](https://profiles.wordpress.org/zeno001/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/insert_with_markers/#comment-5740)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Finsert_with_markers%2F%23comment-5740)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Finsert_with_markers%2F%23comment-5740)
 4.  I don’t think this is classed as a bug but worth noting.
 5.  When splitting out the htaccess file, the code searches for: `$start_marker = "#
     BEGIN {$marker}";` and will find the first instance of it in the file, regardless
     of where it is and even if this is in a commented line before the actual WordPress
     section.
 6.  For example, if I add the [code suggested by Wordfence](https://www.wordfence.com/help/advanced/system-requirements/litespeed/)
     to solve an issue with Wordfence scans and updates and LiteSpeed, and I want to
     add a comment to explain what this code does and where it needs to go (ie before
     the WordPress section), I may add:
 7.      ```php
         # Add fix to stop Litespeed aborting Wordfence scans and updates
         # https://www.wordfence.com/help/advanced/system-requirements/litespeed/
         # Insert just before # BEGIN WordPress
         ... Wordfence noabort code...
         ```
     
 8.  The `# BEGIN WordPress` in this comment is found first by insert_with_markers 
     and the new WordPress section is appended to the next line, overwriting the Wordfence
     noabort code.
 9.  The workaround is not to have `# BEGIN WordPress` in a comment before the WordPress
     section, but ideally the function would be altered so that it only matches # BEGIN
     WordPress at the start of a line.
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Finsert_with_markers%2F%3Freplytocom%3D5740%23feedback-editor-5740)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Finsert_with_markers%2F)
before being able to contribute a note or feedback.