• Resolved Fact Maven

    (@factmaven)


    I’ve been able to add custom rules to my .htaccess through my plugin using the following function:

    add_action( 'init', 'dsbl_htaccess' );
    
    function dsbl_htaccess() {
                require_once( ABSPATH . '/wp-admin/includes/misc.php' );
                $rules = array();
                $rules[] = '<Files xmlrpc.php> # Disable XML-RPC';
                $rules[] = 'Order allow,deny';
                $rules[] = 'Deny from all';
                $rules[] = '</Files>';
                $rules[] = '';
                $rules[] = '<Files wlwmanifest.xml> # Disable Windows Live Writer';
                $rules[] = 'Order allow,deny';
                $rules[] = 'Deny from all';
                $rules[] = '</Files>';
                $htaccess_file = ABSPATH . '.htaccess';
                insert_with_markers( $htaccess_file, 'Fact Maven', ( array ) $rules );
            }

    As a result, the following is added to the .htaccess:

    # BEGIN Fact Maven
    <Files xmlrpc.php> # Disable XML-RPC
    Order allow,deny
    Deny from all
    </Files>
    
    <Files wlwmanifest.xml> # Disable Windows Live Writer
    Order allow,deny
    Deny from all
    </Files>
    # END Fact Maven

    However, I’d like to know how I can have these rules removed once the the plugin is deactivated. After doing some research online, I’m aware that I would have to use the register_deactivation_hook hook which will run a function when I deactivate a plugin.

    But after that I am still lost on trying to figure out where to go after that. If someone could provide me with some guidance, I would appreciate it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi

    You can try:

    function myplugin_deactivate()
    {
        remove_action( 'generate_rewrite_rules', 'dsbl_htaccess' );
        $GLOBALS['wp_rewrite']->flush_rules();
    }
    
    register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

    All the best!

    Thread Starter Fact Maven

    (@factmaven)

    Thanks for the input. I’m also put my plugin in a class structure, so I know the formatting has to be done slightly different. This is what I’ve done based on doing some research online and the example you provided above:

    <?php
    if( ! class_exists( 'MyPlugin' ) ) {
      class MyPlugin {
        public function __construct() {
            add_action( 'init', array( $this, 'factmaven_htaccess' ), 10, 1 );
            // Other actions and filers...
        }
        public function factmaven_htaccess() {
            require_once( ABSPATH . '/wp-admin/includes/misc.php' );
            $rules = array();
            $rules[] = '<Files xmlrpc.php>';
            $rules[] = 'Order allow,deny';
            $rules[] = 'Deny from all';
            $rules[] = '</Files>';
            $rules[] = '';
            $rules[] = '<Files wlwmanifest.xml>';
            $rules[] = 'Order allow,deny';
            $rules[] = 'Deny from all';
            $rules[] = '</Files>';
            $htaccess_file = ABSPATH . '.htaccess';
            insert_with_markers( $htaccess_file, 'Fact Maven', ( array ) $rules );
        }
    
        public function factmaven_deactivate() {
            remove_action( 'generate_rewrite_rules', 'factmaven_htaccess' );
            $GLOBALS['wp_rewrite']->flush_rules();
        }
      }
    }
    
    if( class_exists( 'MyPlugin' ) ) {
      $plugin = new MyPlugin;
      register_deactivation_hook( __FILE__, array( 'MyPlugin', 'factmaven_deactivate' );
    }

    Unfortunately, that doesn’t seem to work, but no errors are showing up when I activate/deactivate my plugin. It looks like I am close. Any suggestions?

    Moderator bcworkz

    (@bcworkz)

    The deactivation hook will fail because you never added a callback to ‘generate_rewrite_rules’. You added it to ‘init’. This too is an issue because you end up writing the same content over and over on every page request. While it works, it is far from efficient.

    I would think writing the content once upon activation with register_activation_hook() is all that is needed. Then removing the content on deactivation is a matter of calling insert_with_markers() with an empty array so nothing appears between the begin and end tags.

    Thread Starter Fact Maven

    (@factmaven)

    Thanks for pointing that out. Also, does it matter if I put the (de)activation hooks in the __construct or outside of the class?

    Moderator bcworkz

    (@bcworkz)

    It may matter. If your plugin instantiates the class when it’s loaded, then it wouldn’t matter because __construct() is always executed. If there is a chance the class is not instantiated, then you cannot rely on __construct() being executed, so the hooks should be registered through other means.

    Thread Starter Fact Maven

    (@factmaven)

    Closing topic

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Remove rules from .htaccess through a plugin’ is closed to new replies.