Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit .htaccess with Wordpress Plugin API? [closed]

Tags:

php

wordpress

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!

like image 540
arodebaugh Avatar asked Mar 23 '16 14:03

arodebaugh


People also ask

Does WordPress overwrite htaccess?

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.

Can I edit the .htaccess file by FTP or Shell?

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.


1 Answers

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/

like image 175
gmaniac Avatar answered Oct 03 '22 14:10

gmaniac