Remove rewrite endpoint on deactivation?
All you need is flush the rewrite rules on deactivation. For example, in the main plugin file: register_deactivation_hook( __FILE__, function() { flush_rewrite_rules(); });
All you need is flush the rewrite rules on deactivation. For example, in the main plugin file: register_deactivation_hook( __FILE__, function() { flush_rewrite_rules(); });
You cannot do this by just adding a rewrite rule, you need to change every recorded rules. Here is a simple way to do this: function my_rewrite_rules($rules) { $new_rules = array(); $new_rules[‘(?:de|en)/?$’] = ‘index.php’; foreach ($rules as $key => $val) { $key = ‘(?:de|en)/?’ . $key; $new_rules[$key] = $val; } return $new_rules; } add_filter(‘rewrite_rules_array’, ‘my_rewrite_rules’, … Read more
This is NOT mod_rewrite rule, it’s WordPress rewrite rule and it proposes to parse variables from URL to wp_query (most of the time). You can apply your logic during parse_query filter hook., something like this. add_filter( ‘parse_query’, function( WP_Query $wp_query ) : WP_Query { foreach ( [ ‘symbol’, ‘exchange’ ] as $key ) { if … Read more
You can’t do that with a rewrite rule, the regular page rule already matches, but you can intercept the data that’s extracted from that rule before it gets transformed into a query. We do that by hooking the parse_request action, where we can check for a custom slug, and modify the request object directly. I … Read more
Query parameters added using add_rewrite_endpoint() are available using get_query_var(). So if you register /en/ as an endpoint: function wpse_318560_en_endpoint(){ add_rewrite_endpoint(‘en’, EP_ALL); } add_action(‘init’, ‘wpse_318560_en_endpoint’ ); And don’t flush rewrite rules on init. Rewrite rules only need to be flushed once. See this note in the developer docs for a way to properly flush rewrite rules … Read more
Your code should (and does from quick check) work in principle. You should not be flushing permalink every page load, just once. Use rewrite helper like Monkeyman Rewrite Analyzer to see if rules are being generated, what they are, and how are they matching to real URLs in your site.
Try this. You also don’t want to include $wp_rewrite->flush_rules() inside your function as that would flush on every page load. add_action(‘rewrite_rules_array’, ‘new_rewrite_rules’); function new_rewrite_rules($rules){ $newrules = array(); $newrules[ ‘^lists/([^/]*)/([^/]*)/?’ = ‘index.php?lists=$matches[1]&post=$matches[2]’; return $newrules + $rules; } If you want to flush the rules only once then try this: add_action(‘init’, ‘flush_new_rule’); function flush_new_rule(){ global $wp_rewrite; if(is_array(get_option(‘rewrite_rules’)) … Read more
Expanding on my comment: The Rewriting API is for rewriting URLs, not redirecting. So the URL won’t change. You’re just telling WordPress that /testpage/ should load page_id 81, but the URL will stay /testpage/. Regardless, I tried the below, (where 501 is an ID that exists on my install) and it worked fine, loading that … Read more
add_rewrite_rule is for converting a URL structure to query vars and routing requests through index.php, you can’t point to other URLs and use flags like you would in htaccess rewrites. for what you’re trying to achieve, doing it via htaccess is the simplest way. your rule should work if you just remove the leading slash: … Read more
First of all, always develop with debugging enabled so you can see errors. This will reveal the first issue: Undefined variable: matches. Your rule should be in single quotes so it’s passed as a string and the $matches variable is not expanded. The second issue is that there is no category query var, if you’re … Read more