add_rewrite_rule, plugin activation and plugin deactivation

The problem with calling flush_rewrite_rules() is that the rules instantly get regenerated, while your plugin’s hooks are still active. What I usually do, because it’s the simplest route to success, is delete the rewrite_rules option on activation/deactivation. The rules will then get regenerated on the next pageload instead of the current one: register_deactivation_hook( __FILE__, ‘my_plugin_deactivation’); … Read more

permastruct for custom post type not working in one of four cases

You’re currently placing your rewrite rules in the global scope, which means they execute as soon as your file is loaded, which may be too early. Instead try adding them on the init hook, or rewriting them to use the generate_rewrite_rules filter instead, e.g.: add_action(‘generate_rewrite_rules’, ‘themes_dir_add_rewrites’); function themes_dir_add_rewrites() { $theme_name = next(explode(‘/themes/’, get_stylesheet_directory())); global $wp_rewrite; … Read more

WordPress custom taxonomy URL rewrite on spelling errors

It also doesn’t matter which URL part is changed, except the last one. So this still shows the content of the original page. Yes, because your taxonomy’s rewrite is hierarchical with the permalink structure /treatment/<term slug> (e.g. /treatment/psychosomatic) or /treatment/<term slug>/<term slug>/… (if the term is a child term, e.g. /treatment/psychosomatic/psychosomatic-dysfunction), so as long as … Read more

How to change author base without front

This is the best solution I have come up with but I welcome better ways: function change_author_permalinks() { global $wp_rewrite; $wp_rewrite->author_base=”connect/member”; $wp_rewrite->author_structure = “https://wordpress.stackexchange.com/” . $wp_rewrite->author_base . ‘/%author%’; add_rewrite_rule(‘connect/member/([^/]+)/?$’, ‘index.php?author_name=$matches[1]’, ‘top’); } add_action(‘init’,’change_author_permalinks’);

rewrite get parameter with custom post type

I think it should work. First you need to register your rewrite rule and the parameter: function my_init() { add_rewrite_rule( ‘gallery/(.+?)/?$’, ‘index.php?post_type=gallery&param=$matches[1]’, ‘top’ ); add_rewrite_tag(‘%param%’,'([^&]+)’); } add_action(‘init’, ‘my_init’); Then you can read the parameter value from the query: $param = get_query_var(‘param’); Don’t forget to flush the rewrites. (just view the rewrite settings page to flush … Read more