You can change rules to rewrite your type of posting as well as change your structure. Imagine that your permalink structure is like: /locations/%k7_locations%
To add a custom URL-/permalink-structure, this function must be called on every request (not only once). I usually do this in the ‘init’ hook.
However, the related function flush_rewrite_rules() should only be called when the permalink structure changes.
Sample:
add_action( 'init', 'wpdocs_custom_permalink' );
function wpdocs_custom_permalinks() {
// Register the permastruct and rewrite tag on every page load:
add_permastruct( 'legal', 'legal/%my_slug%', [ 'ep_mask' => EP_PERMALINK ] );
add_rewrite_tag( '%my_slug%', '([^/]+)', "post_type=page&name=" );
// Only flush rewrite rules, if your permalink changed (e.g. on plugin activation):
if ( $has_changed ) {
flush_rewrite_rules();
}
}
You must log in before being able to contribute a note or feedback.
You can change rules to rewrite your type of posting as well as change your structure. Imagine that your permalink structure is like:
/locations/%k7_locations%you can switch to
/test/%message%output:
To add a custom URL-/permalink-structure, this function must be called on every request (not only once). I usually do this in the ‘init’ hook.
However, the related function
flush_rewrite_rules()should only be called when the permalink structure changes.Sample: