Here is a random example from one of my slides. Note: tag supposes to be in your type permastruct. random
itself will need to be added to public query vars, but let this be a homework, and analyzed somehow.
add_rewrite_tag( '%random%', '(.+)', 'random=' );
add_filter( 'available_permalink_structure_tags', function ( array $tags ) : array {
$tags['random'] = 'some random thing';
return $tags;
});
// or 'post_link' , or any other link filter
// note, you can have more arguments passed to callback,
// but this depends on link.
add_filter( 'post_type_link', function( string $permalink ) : string {
if (false === strpos( $permalink, '%random%' ) ) {
return $permalink;
}
return str_replace( '%random%', rand(1000, 4000), $permalink);
});