/wp-admin/includes/misc.php 包括功能insert_with_markers() :
/**
* Inserts an array of strings into a file (.htaccess ), placing it between
* BEGIN and END markers.
*
* Replaces existing marked info. Retains surrounding
* data. Creates file if none exists.
*
* @since 1.5.0
*
* @param string $filename Filename to alter.
* @param string $marker The marker to alter.
* @param array|string $insertion The new content to insert.
* @return bool True on write success, false on failure.
*/
function insert_with_markers( $filename, $marker, $insertion ) {
insert_with_markers() 检查文件是否存在(&P);是可写的,必要时创建它。它将您的数据插入到文件中
# BEGIN 和
# END 标记,替换那些标记之间已经存在的任何线。可以向其传递一个行数组或带换行符的字符串。确保您的标记名是唯一的,以避免与WP core或其他插件发生冲突。
在同一个核心文件中,save_mod_rewrite_rules() 显示WP的设置方式$filename, $marker &;$insertion 对于默认的permalink规则,从该函数中大量窃取:
if ( is_multisite() ) {
// I\'m not going to go here
} else {
// Ensure get_home_path() is declared.
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
$home_path = get_home_path();
$htaccess_file = $home_path . \'.htaccess\';
$lines = array(
\'# These are\',
\'# the lines\',
\'# I want to add\',
);
if ( insert_with_markers( $htaccess_file, \'wpse_331608\', $lines ) ) {
// Celebrate your success
} else {
// Deal with your failure
}
}
如果您的规则是用于重写的,那么WP还具有一个helper函数来检查您使用Apache的情况
mod_rewrite 已启用:
if ( got_mod_rewrite() ) {
if ( insert_with_markers( $htaccess_file, \'wpse_331608\', $lines ) ) {
// Celebrate your success
} else {
// Deal with your failure
}
}