Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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;
   unset($wp_rewrite->non_wp_rules['the-string-you-passed-as-param-to-add-rewrite-rule']);
   flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
share|improve this answer
    
uhh... I was not aware of add_rewrite_rule function... I think I will rewrite to use this, looks best thanks –  nekojira Feb 23 '13 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? –  nekojira Feb 23 '13 at 12:29
    
Did you try the example on add_rewrite_rule() codex page? Does this get added to .htacces? –  s1lv3r Feb 23 '13 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! –  nekojira Feb 23 '13 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.

share|improve this answer
    
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 '13 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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