Restrict access to admin but allow admin_post hook

I’m using this hook to allow only admin roles access dashboard

add_action( ‘admin_init’, function() {
if (defined(‘DOING_AJAX’) && DOING_AJAX) {
return;
}

if (!current_user_can(‘manage_options’)) {
wp_redirect(home_url(‘/meu-perfil’));
exit();
}
});

Now I need to run a function when a form is submitted on front end, like so:

function editUser(){
error_log(‘message’);
}

add_action(‘admin_post_nopriv_add_foobar’, ‘editUser’);
add_action(‘admin_post_add_foobar’, ‘editUser’);

But the first hook is blocking the second one.

Read more here:: Restrict access to admin but allow admin_post hook

Leave a Reply

Your email address will not be published. Required fields are marked *