3

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.

1 Answer 1

3

All you need to do is to modify your method of restricting users.

add_action( 'admin_init', function() {
    if ( (defined('DOING_AJAX') && DOING_AJAX) || ( strpos($_SERVER['SCRIPT_NAME'], 'admin-post.php') ) ) {
        return;
    }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.