The $query parameter is optional. If it is omitted you must ensure that you call this on, or before, the 'init' hook. This is because $query defaults to $tag=, and for this to work a new query var has to be added.
Parameters
$tagstring
Name of the new rewrite tag.
$regexstring
Regular expression to substitute the tag for in rewrite rules.
$querystringoptional
String to append to the rewritten query. Must end in '='. Default empty.Default: ''
162functionadd_rewrite_tag($tag,$regex,$query=''){163// Validate the tag's name.164if(strlen($tag)<3||'%'!==$tag[0]||'%'!==$tag[strlen($tag)-1]){165return;166}167168global$wp_rewrite,$wp;169170if(empty($query)){171$qv=trim($tag,'%');172$wp->add_query_var($qv);173$query=$qv.'=';174}175176$wp_rewrite->add_rewrite_tag($tag,$regex,$query);177}
History
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rewrite.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.