I have a plugin that inserts some rewrite rule in the .htaccess file. However, upon deactivating it, doesn't remove said rule. The rule is nested in # BEGIN My plugin and # END My plugin.

I want to create an uninstall.php file for this plugin to remove the rule created on acrivation.

I've never done this before. I suppose I can get the .htaccess with

$htaccess = dirname(__FILE__) . '/../../../.htaccess';

should I use a regex or is there a proper way to remove a specific rule from the .htaccess?

2 Answers 2


How do you add your rewrite rule?

If you are adding it manually (opening the .htaccess and adding the lines by yourself in php) then you are also supposed to remove it manually. As you suggested the best way would then be to use regex.

If the rewrite rule was added through the Wordpress API using add_rewrite_rule() you are able to remove it using flush_rewrite_rules(). You could use it like this:

register_activation_hook( __FILE__, 'myplugin_activate' );

function myplugin_deactivate() {
   global $wp_rewrite;
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
  • uhh... I was not aware of add_rewrite_rule function... I think I will rewrite to use this, looks best thanks
    – unfulvio
    Feb 23, 2013 at 11:56
  • I tried to use add_rewrite_rule() with my register_activation_hook but it doesn't add any rewrite rule to my .htaccess, add_rewrite_rule( '^(.+)(jpeg|jpg|gif|png)$', basename(WP_CONTENT_DIR) . '/plugins/direct-images-redirect/redirect.php?file=$1$2 [L]', 'bottom' ); - am I doing it wrong?
    – unfulvio
    Feb 23, 2013 at 12:29
  • Did you try the example on add_rewrite_rule() codex page? Does this get added to .htacces?
    – s1lv3r
    Feb 23, 2013 at 13:20
  • oh thanks I've figured out, there was a space missing in my rewrite rule for some reason wp refused to write that (maybe because invalid), glad it works and also your deactivation code works thanks!
    – unfulvio
    Feb 23, 2013 at 13:26

With the way the plugin uninstall process works, this kind of things can not be done as you will need to get credentials to be able to write to the .htaccess before performing the uninstall itself, and you can't break the uninstall process into stages. This kind of functionality can be achieved only when the plugin is still active.

  • You are right in the case when the file is not writeable in php and the ftp wrapper has to be used. Then you need to fetch the credentials which you are not able to get at this stage. :-(
    – s1lv3r
    Feb 23, 2013 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.