Documentation

wp-hide/module_mod_rewrite_rules

Posted in: Getting Started (4) How To (17) Plugin Options Explained (41) Actions / Filters (34)      Code Example (18)  

Name: wp-hide/module_mod_rewrite_rules
Type: Filter
Arguments:
(str) $module_mod_rewrite_rules
(object) $_class_instance

The wp-hide/module_mod_rewrite_rules filter provides a way to customize the rewrite rules generated by the WP Hide plugin for a specific option. These rules, created by the plugin’s core functionality and stored on the server, define how the selected option impacts the data, files or user behavior. By utilizing this filter, developers can fine-tune or extend the generated rules to better align with specific requirements, ensuring optimal performance and compatibility with custom setups.

The following code example whitelists the wp-activate.php option for a specific IP address, ensuring access is granted only to the specified source. This filter can be applied to any other option within the plugin, allowing for granular control over rewrite rules. By leveraging this approach, you can conditionally permit or restrict specific rewrites based on custom criteria, enhancing security and flexibility in how URL structures are managed.


    add_filter ( 'wp-hide/module_mod_rewrite_rules', 'wp_hide_module_mod_rewrite_rules', 10, 2 );
    function wp_hide_module_mod_rewrite_rules( $module_mod_rewrite_rules, $_class_instance )
        {
            //catch only a specific component
            if ( $_class_instance->id   !== 'root-files' )   
                return $module_mod_rewrite_rules;
            
            if ( empty ( $module_mod_rewrite_rules )    ||  ! isset ( $module_mod_rewrite_rules['rewrite'] )    ||  empty ( $module_mod_rewrite_rules['rewrite'] ) )
                return $module_mod_rewrite_rules;
                
            //allow the wp-activate for a specific IP
            
            $whitelist_ip   =   '100.5.54.50';
            $custom_rewrite_line    =  "RewriteCond %{REMOTE_ADDR} !^".  str_replace(".",'\.', $whitelist_ip )  ."$\n"; 
            
            $search_line    =   "RewriteRule ^wp-activate.php /index.php?wph-throw-404 [L]";
            $custom_rewrite_line    .=   $search_line;
            
            $pattern = "/" . preg_quote( $search_line, '/') . "/m";
            $updated_rules = preg_replace( $pattern, $custom_rewrite_line, $module_mod_rewrite_rules['rewrite'] );

            // If replacement didn't happen, keep original content
            if ( $updated_rules !== null )
                $module_mod_rewrite_rules['rewrite'] = $updated_rules;

            return $module_mod_rewrite_rules;    
        }

The above code should be placed inside a custom file on /wp-content/mu-plugins/ folder or theme functions.php or a custom plugin.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Scroll to top