我不确定是否有一个标准的方法或功能来做到这一点,但像这样的东西我认为将是一个很好的起点。。。
function remove_marker($contents, $marker) {
$posa = strpos($contents, \'# BEGIN \'.$marker);
$posb = strpos($contents, \'# END \'.$marker) + strlen(\'# END \'.$marker);
$newcontent = substr($contents, 0, $posa);
$newcontent .= substr($contents, $posb, strlen($contents));
return $newcontent;
}
$marker = \'marker\';
// rewrite file using WP Filesystem
ob_start();
$creds = request_filesystem_credentials(\'\', false, false, dirname($htaccess), null, true);
ob_end_clean();
if ($creds) {
$filesystem = WP_Filesystem($creds, dirname($htaccess), true);}
if ($filesystem) {
global $wp_filesystem;
$contents = $wp_filesystem->get_contents($htaccess);
$newcontents = remove_marker($contents, $marker);
$writeresult = $wp_filesystem->put_contents($htaccess, $newcontents, FS_CHMOD_FILE);
}
}