• Hi All,
    I’m trying to write a plugin for WordPress 2.6 that will add a rewrite rule. I’m following the instructions listed at this post http://wordpress.org/support/topic/208830?replies=6

    [code]

    function wftp_rewrite() {
      add_rewrite_rule('wftp/([^/]+)/?$', 'index.php?wftp=$matches[1]','top');
    }
    function wftp_vars($public_query_vars) {
        $public_query_vars[] = 'wftp';
        return $public_query_vars;
    }
    add_filter('query_vars', 'wftp_vars');
    add_action('init', 'wftp_rewrite');

    [/code]

    I think my code is almost identical, but it doesn’t look like WP is adding the rewrite to the database. Going to the Links section of WP admin doesn’t do the trick either.

    Is there something else I need to do in my code to flush the rewrite_rules?

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter bennybobw

    (@bennybobw)

    Ok, so I’ve figured out that I have to use a different method. Here’s my working code, but I don’t understand why.

    function wftp_flush_rewrite() {
      global $wp_rewrite;
      $wp_rewrite->flush_rules();
    }
    function wftp_vars($public_query_vars) {
        $public_query_vars[] = 'wftp';
        return $public_query_vars;
    }
    function wftp_add_rewrite_rules($wp_rewrite) {
      $new_rules = array(
         'wftp/(.+)' => 'index.php?wftp=' .
           $wp_rewrite->preg_index(1));
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    add_action('init', 'wftp_flush_rewrite');
    add_filter('query_vars', 'wftp_vars');
    add_action('generate_rewrite_rules', 'wftp_add_rewrite_rules');
    add_action('parse_query', 'wftp_init');

    Solution taken from:
    http://codex.wordpress.org/Custom_Queries

    I’d appreciate it if someone could explain why I can’t use add_rewrite_rule in this way, since it seems like others are doing it. e.g. http://danmarvelo.us/older/2007/5/30/custom-rewrite-wordpress-add_rewrite_rule-add_rewrite_tag/

    Thanks!

Viewing 1 replies (of 1 total)

The topic ‘Plugin dev add_rewrite_rule – not adding to db?’ is closed to new replies.