I found that currently this is the only reliable solution:
if (is_admin()){
header('Location: '. get_permalink());
exit();
}
else
add_action('template_redirect', function (){
wp_redirect(get_permalink());
exit();
});
In the case of backend pages the template_redirect
does not seem to be supported. If I use wp_redirect
the redirection does not happen (maybe it is a bug) or the function is not defined yet.
Another alternative solution which works by both the frontend and backend pages is
add_action('init', function (){
header('Location: '. get_permalink());
exit();
}, 999);
If I add it with normal priority, it does not run, so better to add it with higher priority than the original init
where we add it from. This makes sense, because the number isn’t really the priority, but the running order of the callbacks. So when I add something from a 10 priority callback (which is the default), then it won’t run if its priority is less than 10 or equals to 10.
Yet another solution is
add_action(is_admin()?'init':'template_redirect', function (){
header('Location: '. get_permalink());
exit();
}, 999);
I like this the best so far, because template_redirect
certainly runs later in the case of frontend pages than init
and it works in the case of admin pages as well.