If specified user is in wordpress admin then redirect EXCEPT if the admin page is a woocommerce shop order
I'm trying to redirect a specified user to a specified URL if that user tries to access WordPress admin except if it is a WooCommerce order post. I have tried many ideas in functions.php but none work. In these two ideas below, the user is redirected to the homepage regardless.
Idea 1
add_action('admin_init', 'redirect_admin_user_except_orders');
function redirect_admin_user_except_orders() {
$user = wp_get_current_user();
if( $user && isset($user->user_email) && 'info@example.com' == $user->user_email ) {
global $post_type;
if( is_admin() && 'shop_order' !== $post_type ){
wp_redirect( '/', 301 );
exit;
}
}
}
Idea 2
add_action('admin_init', 'redirect_admin_user_except_orders');
function redirect_admin_user_except_orders() {
$user = wp_get_current_user();
if( $user && isset($user->user_email) && 'info@example.com' == $user->user_email ) {
if ( is_admin() && ! is_singular('shop_order') ) {
wp_redirect( '/', 301 );
exit;
}
}
}