Andrew
Andrew

Reputation: 2164

WP URL Re-writing plugin

I am trying to integrate a few non-wordpress PHP pages into an existing Wordpress site. Ideally, I want to rewrite anything request that looks like this: 'domain.com/books/bookname' to 'domain.com/catalog.php?title=bookname'. I have been messing around with the WP Rewrite API for several hours, but I just can't get anything to work. Here is my current plugin code:

register_activation_hook( __FILE__, 'wbm_catalog_activate' );
function wbm_catalog_activate() {
    wbm_catalog_add_rules();
    flush_rewrite_rules();
}

// Flush when deactivated
register_deactivation_hook( __FILE__, 'wbm_catalog_deactivate' );
function wbm_catalog_deactivate() {
    flush_rewrite_rules();
}

add_action( 'init', 'wbm_catalog_add_rules' );
function wbm_catalog_add_rules() {
    add_rewrite_rule( 'books/([^/]+)/?',
        'catalog.php?title=$matches[1]', 'top' );
}

I can even inspect the $wp_rewrite object and see my new rule added, but whenever I try to visit a rewritten url, wordpress acts as though the rule were not there.

I have been banging my head up against a brick wall, and I would appreciate any help you could give!

Upvotes: 1

Views: 205

Answers (2)

Andrew
Andrew

Reputation: 2164

I was unable to ever resolve this issue. The site that I was working on was part of an old WordPressMU setup (2.8) with a number of sites. I did not have access to the .htaccess that the MU install was using, and it was not passing any requests through to the .htaccess file that I could edit.

Upvotes: 0

tszming
tszming

Reputation: 2084

The problem is:

The statement flush_rewrite_rules is executed before add_rewrite_rule and therefore the new cache generated does not include your stuffs in add_rewrite_rule.

You can verify with the value in your DB

select option_name, option_value from wp_options where option_name = 'rewrite_rules';

Upvotes: 1

Related Questions