I have a standard form in WordPress and I am struggling with restricting access to the WordPress backend and properly working forms, because the forms are processed over wp-admin and if this is restricted, forms are not processed.
<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
...
</form>
Restricting backend access:
// Restrict users to enter backend
add_action('init', 'restrict_backend_access');
function restrict_backend_access() {
if (is_admin() && current_user_can('subscriber') && !(defined('DOING_AJAX') && DOING_AJAX)){
wp_redirect( '/dashboard/' );
exit;
}
}
Am I missing something? I know that this is possible, because I build it many times, but this time something is different and I don’t know why.
Thank you!
2
Answers
Please find correct code as below:
You can set redirect as per your requirement.
As for example replace
with
You can add a hidden field to the form
then use it to bypass the redirection