使用Wordpress Plugin API编辑.htaccess?

aro*_*ugh 5 php wordpress wordpress-plugin

我想写一个wordpress插件,需要更改.htaccess文件.我怎么用PHP做到这一点.我之前已经看过其他插件,但我无法弄清楚它是如何完成的.谢谢!

gma*_*iac 9

wordpress中用于更新.htaccess文件的功能是insert_with_markers需要三个参数.

insert_with_markers ( string $filename, string $marker, array|string $insertion )
Run Code Online (Sandbox Code Playgroud)

在本教程中,您可以编写类似这样的内容

// Get path to main .htaccess for WordPress
$htaccess = get_home_path().".htaccess";

$lines = array();
$lines[] = "RewriteBase /foobar";

insert_with_markers($htaccess, "MyPlugin", $lines);
Run Code Online (Sandbox Code Playgroud)

这在您的.htaccess文件中看起来像这样

# BEGIN MyPlugin
RewriteBase /foobar
# END MyPlugin
Run Code Online (Sandbox Code Playgroud)

这是wordpress'该功能文档的链接

https://developer.wordpress.org/reference/functions/insert_with_markers/