• Resolved tunamaxx

    (@tunamaxx)


    I’ve been plodding my way through creating a custom ‘non_wp_rules’ for the past day or so. Obviously, http://codex.wordpress.org/Function_Reference/WP_Rewrite has been a huge help.

    I am able to to get the rewrite working as intended, but only by going to options-permalink.php and manually saving out the Permalinks options. At that point, my .htaccess file is updated, including my rule right after RewriteBase and before the first RewriteCond:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     RewriteRule ^find/(this)$ /addit.php?here=$1 [QSA,L]
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    My question is how do I mimic the manual save step at options-permalink.php when saving my theme options to enable / disable the non-WordPress rewrite?

    Here is my code in my theme’s functions.php:

    function ftc_flush_rewrites() {
     global $wp_rewrite;
     $wp_rewrite->flush_rules();
    }
    
    function ftc_add_rewrites() {
     global $wp_rewrite;
     $ftc_new_non_wp_rules = array(
    	'find/(this)' => '/addit.php?here=$1',
     );
    
     $wp_rewrite->non_wp_rules = $ftc_new_non_wp_rules + $wp_rewrite->non_wp_rules;
    }
    
    add_action('init', 'ftc_flush_rewrites');
    add_action('generate_rewrite_rules', 'ftc_add_rewrites');

    I understand that the $wp_rewrite->flush_rules(); in my ftc_flush_rewrites function is supposed to write to the .htaccess file with the implied ‘hard’ flush, but why is it not actually being written until I force it by a manual save at options-permalink.php?

    Sorry this is such a long post! Just trying to be thorough. 😉

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter tunamaxx

    (@tunamaxx)

    I wonder if I should take the rewrite functionality out of the theme and build it ito an acompanying plugin? I’d prefer to keep it all in the theme though…

    Thread Starter tunamaxx

    (@tunamaxx)

    Testing… is this thing on? 😉

    Hi TunaMaxx, have you been able to solve the problem? I’ve got a similar issue, really don’t wanna dig into the admin panel code to see what happens..

    Thread Starter tunamaxx

    (@tunamaxx)

    No, unfortunately I have not been able to solve it. I did dig into the code a little bit, and from what I can figure out, the key is in the:

    $wp_rewrite->flush_rules()

    It is supposed to write out the changes to .htaccess if the first arg is NOT set to false.

    Thread Starter tunamaxx

    (@tunamaxx)

    Hmm… can’t edit my own post to add this, so I thought I’d point out some interesting links. I still haven’t found a solution, but these have shed some light:

    Adding rewrite rules and $wp_rewrite->flush_rules():
    http://wordpress.org/support/topic/313946?replies=7
    "This is a bug in the way we store rewrite_rules in "transients"
    instead of options like we used to. I think our fix for WP 2.9 is
    going to be to revert to the old method.
    
    http://core.trac.wordpress.org/ticket/10981"
    Correct usage of add_rewrite in plugin:
    http://wordpress.org/support/topic/226529?replies=3
    "Second, the problem is that add_rewrite_rule needs to come before
    you flush the rules. I know it seems unintuitive, but what happens
    when you flush the rules is that they're immediately regenerated,
    at which point WordPress says, "hmm, what rewrite rules do I need
    to include?"

    I found something more.. flush_rules has $hard = true by default, that means that it is supposed to write to .htaccess, but it doesn’t because the save_mod_rewrite_rules function doesn’t exist at that moment. I tried including both wp-admin/includes/misc.php and wp-admin/includes/file.php and it seems to be trying to write something, but with no success 🙁 heh.. Let me know if you get to something ..

    TunaMaxx, I think I just found a solution … Give me a sec, lemme wrap it up in a post.

    Ah, nevermind. Didn’t catch the theme options part. I had to rebuild my .htaccess whenever a page or post is published and flush_rules(true) works fine in publish_post hook …

    Thread Starter tunamaxx

    (@tunamaxx)

    I’m still scouring code and support forums looking for a solution. Between the two of us, we might just come up with something!

    Thread Starter tunamaxx

    (@tunamaxx)

    I have been tearing my hair out on this. Is there anyone that can offer assistance? PLEASE?

    Thread Starter tunamaxx

    (@tunamaxx)

    Success! So simple once you catch on…

    The all important $wp_rewrite->flush_rules() can only be called from the Admin side. Makes sense, right? You wouldn’t want to be calling it every single page load.

    This whole time, my code has been pretty much correct except for where I was calling the flush. Since it needs to be called from the Admin side, the hook is ‘admin_init‘:

    add_action('admin_init', 'ftc_flush_rewrites');

    Here is the complete functioning sample code:

    function ftc_flush_rewrites() {
     global $wp_rewrite;
     $wp_rewrite->flush_rules();
    }
    
    function ftc_add_rewrites() {
     global $wp_rewrite;
     $ftc_new_non_wp_rules = array(
    	'find/(this)' => '/addit.php?here=$1',
     );
    
     $wp_rewrite->non_wp_rules = $ftc_new_non_wp_rules + $wp_rewrite->non_wp_rules;
    }
    
    add_action('generate_rewrite_rules', 'ftc_add_rewrites');
    add_action('admin_init', 'ftc_flush_rewrites');

    Thanks for share =D That worked fine for me!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Writing $wp_rewrite->non_wp_rules to .htaccess?’ is closed to new replies.