Edit .htaccess with WordPress Plugin API?

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!

Related posts

1 comment

  1. 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/

Comments are closed.