最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Restrict access to admin but allow admin_post hook

matteradmin9PV0评论

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.

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.

Share Improve this question edited Nov 7, 2018 at 20:46 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 7, 2018 at 11:41 MarceloMarcelo 473 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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();
    }
}
Post a comment

comment list (0)

  1. No comments so far