Connect with us

How to safely update .htaccess from a WordPress plugin?


How to safely update .htaccess from a WordPress plugin?

Sometimes during plugin development you may need to make an update to the .htaccess file. This can be rather daunting when you start to think about the unknown. What is currently there? Does the file exist? Can I write to it? Luckily WordPress has a builtin function to help assist with this, “insert_with_markers()” in misc.php.

The function relies on markers in the .htaccess to identify which part it needs to update. For example the WordPress updates for permalinks has “WordPress” as the marker and looks something like:

Adding your own section is easy. The function takes three parameters:

  1. filename – In this case, the full path of .htaccess
  2. marker – A unique marker that makes sense for your plugin.
  3. insertion – An array with each element being a line of text.

Here is an example:

This would create an addition to .htaccess that looks like this:

One important point to mention is that get_home_path() and insert_with_markers() are only available while the plugin is executing in the admin interface. For example your activation function for you plugin would be a great place to put this. During deactivation, sending a blank array for the insertion parameter will remove the configuration.

No votes yet.
Please wait...
Related:  A short history of HTML
Continue Reading
You may also like...

We are a WordPress plugin developer company that focuses on useful WordPress plugin creation and empowering people to earn passive incomes from their blogs. We build unique and groundbreaking plugins that will revolutionize your blog!

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in coding

Sign up for our newsletter and get a free affiliate e-book!

About Me:

Szabi Kisded

Hi, my name is Szabi and I'm documenting my journey selling plugins on CodeCanyon. I will show you every step of it: learning to code, plugin ideas, WordPress stuff and more. Read more…

Mega Plugin Bundle:

CodeCanyon Portfolio:

Learn to Code Plugins:

Latest Promotions:

Recommended Theme:

Start Your Own Blog:

Recommended VPN:

Translate Your Blog:

AdSense Alternative:

Best Article Spinner:

Popular Posts:

Latest Posts:

To Top

Privacy Preference Center