I need to redirect logged-in and non-admin users, from page with id 172, to the home of the website. Note that users that are not logged in can actually see the page.
add_action('admin_init', 'xyz');
function xyz() {
if( is_page( 172 ) ) {
if( is_user_logged_in() && ! current_user_can('update_core') ) {
wp_redirect( home_url() );
exit;
}
}
}
Problem: This code is not working.
admin_init
runs on admin pages, not the front end. The equivalent front end action,init
, is too early to checkis_page
. A safe action for redirection istemplate_redirect
: