Jess McKenzie
Jess McKenzie

Reputation: 8385

Wordpress re write rule

Lets say I have a URI thats portfolio_categories/guttersspouting/

How could I rewrite a function to change it too "products"

Upvotes: 0

Views: 35

Answers (1)

David
David

Reputation: 5937

ok as requested this matches the uri "product" exactly. This is not advisable if releasing code to a third party, as if they create a page called product, it will follow this rule.

Btw I am assuming you mean post_type = page (will also work for posts, see the post_id= in the add_rewrite_rule? You need to change this to the post id of the page you want to redirect to.

add_action('init', 'new_rewrite_rule');
function new_rewrite_rule(){
   // matches product exactly, product/aproduct will fail, etc
   // you need to add your own post_id into the function below. 
   add_rewrite_rule('^product$','index.php?page_id=12','top');  
}

You will need to flush the rewrite rules for this to work (go to the permalinks setting page and just hit the save button)

code version of flush rewrite rules, not a good idea to have it running on wp_loaded, should be on a activation hook really but for testing this will do:

function new_flush_rewrite_rules() {

    flush_rewrite_rules();
}

add_action( 'wp_loaded', 'new_flush_rewrite_rules' );

Just to note you can also do this manually in the post edit screen.

Upvotes: 1

Related Questions