简体   繁体   中英

Rewrite rules for URL prefixes

I am rewriting some URL prefixes (ie category prefix, tag prefix or custom taxonomy prefix) in my Wordpress plugin. I am using Wordpress' rewrite API :

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('init','flushRules');  

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['kategorie/(.+?)/?$'] = 'index.php?category_name=$matches[1]';
    return $newrules + $rules;
}

It is working, but the system URLs on site have not changed, and WP is using the old prefix 'category' from the database. How do I rewrite those URLs as well?

If you want to change the permalink structure for all categories, then you can configure that at Settings -> Permalinks -> Category base .

If you want to change the link for only one or a few categories, you can use the WordPress category_link filter:

function update_permalink( $permalink, $cat_id ) {
    // If $cat_id is category we want to change, modify $permalink
    return $permalink;
}
add_filter( 'category_link', 'update_permalink', 11, 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM