I want to write a wordpress plugin that requires changing the .htaccess file. How do I do that in PHP. I have seen this done before with other plugins but, I can not figure out how it is done. Thanks!
By default, depending on file permissions, WordPress automatically will modify the contents of your site's . htaccess file. It does this on several occasions, adding and/or updating the rewrite rules required for WP's permalink functionality.
There are many ways to edit an . htaccess file. Edit the file on your computer and upload it to the server via FTP. Use an FTP program's "Edit" mode that allows you to edit a file remotely.
The function in wordpress to update the .htaccess
file is insert_with_markers
it takes three parameters.
insert_with_markers ( string $filename, string $marker, array|string $insertion )
in following this tutorial you could write something like this
// Get path to main .htaccess for WordPress
$htaccess = get_home_path().".htaccess";
$lines = array();
$lines[] = "RewriteBase /foobar";
insert_with_markers($htaccess, "MyPlugin", $lines);
That would look like this in the your .htaccess
file
# BEGIN MyPlugin
RewriteBase /foobar
# END MyPlugin
Here is a link to wordpress' documentation of that function
https://developer.wordpress.org/reference/functions/insert_with_markers/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With