2

I want script will add new rules to current .htaccess and user no need to ftp or edit it manually.

Example, I use timthumb for resize image on my theme and want rewrite the URL and current .htaccess will be something like this.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^images/thumb/(.*) timthumb.php?filename=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Question

How to add the new rule to the .htacces on my theme option.

Enable Mod rewrite for Timthumb? : [Yes] [No]

If click [Yes] RewriteRule ^images/thumb/(.*) timthumb.php?filename=$1 will automatic added to current .htaccess

Let me know

4

You can add extra rules right after the ^index\.php$ line via the WP_Rewrite class, more specifically the add_external_rule() method. They are added to the non_rewrite_rules array, which is written in the mod_rewrite_rules() method.

This is a very simple example. You should still flush the rewrite rules (once), either on plugin activation or by visiting the Permalinks page.

add_action( 'init', 'wpse9966_init' );
function wpse9966_init()
{
    global $wp_rewrite;
    // The pattern is prefixed with '^'
    // The substitution is prefixed with the "home root", at least a '/'
    $wp_rewrite->add_external_rule( 'images/thumb/(.*)$', 'timthumb.php?filename=$1' );
}
  • Thank you so much! I got it worked. I just flush the rules on activation theme. – wow Feb 21 '11 at 16:41
  • add_action('admin_init', 'wpse9966_init'); call on theme activation and I add $wp_rewrite->flush_rules(true); in the function and it work like a charm. If without $wp_rewrite->flush_rules(true); this won't work. – wow Feb 21 '11 at 16:52
  • @wow: You should not call flush_rules() on every init, only when the rules change (when the user changes the setting). This is an "expensive" operation, it takes some time. – Jan Fabry Feb 21 '11 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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