1

I'm trying to submit form using:

`admin_post` && `admin_post_nopriv` 

actions. This is a shortcode for users search:

add_shortcode( 'search_users', 'search_users' );
function search_users( $atts ,$content = null ) {
ob_start();?>
<div class="search-box-main-container">
    <form method="POST" id="sul-searchform" action="<?php echo home_url() . '/wp-admin/admin-post.php' ?>" class="search-form">
        <div class="search-box-container">
            <input type="text" class="search-box" name="search" id="sul-s"/>
        </div>

        <div class="submit-container">
            <input type="hidden" name="action" value="search_users_action">
            <input type="submit" class="search-box-submit" name="submit" id="sul-searchsubmit"
                   value="Search"/>
        </div>
    </form>
</div>

<?php
$output = ob_get_contents();
ob_end_clean();
return  $output;
}

And these are the actions:

add_action( 'admin_post_search_users_action', 'list_users' );
add_action( 'admin_post_nopriv_search_users_action', 'list_users' );
function list_users() {
$search = (isset($_POST['search'])) ?sanitize_text_field($_POST['search']) : false;
wp_redirect( home_url() . '/user-listing?search=' . $search );
}

They work only for admin users and unauthenticated users. With authenticated non-admin user, the actions haven't been called. It just redirects to the home page!

Is there another action to handle authenticated non-admin users?

6
  • No, admin_post can be used for all authenticated users, and admin_post_nopriv for all non-authenticated users. But depending on what you want to do, there are other relevant hooks like init and template_redirect which you can use to process submitted form data, redirect the user, etc.
    – Sally CJ
    Commented Sep 12, 2019 at 12:33
  • @SallyCJ So, why doesn't it work with non-admin authenticated users? Is there additional thing to do? Commented Sep 12, 2019 at 13:20
  • What's your code? And how does it not works?
    – Sally CJ
    Commented Sep 12, 2019 at 13:25
  • @SallyCJ I've added the code. Commented Sep 12, 2019 at 15:02
  • 1
    Thanks, @SallyCJ . I found that the issue was due to another plugin on the site. Commented Sep 12, 2019 at 19:35

1 Answer 1

0

I found that the issue was due to another plugin on my site. After deactivating it, everything works well. So, the action admin_post is used for all authenticated users( admins and non-admins ).

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.