admin管理员组

文章数量:1122832

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?

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?

Share Improve this question edited Sep 12, 2019 at 19:14 Matthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges asked Sep 12, 2019 at 11:58 Rowayda KhayriRowayda Khayri 1215 bronze badges 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? – Rowayda Khayri 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. – Rowayda Khayri Commented Sep 12, 2019 at 15:02
  • 1 Thanks, @SallyCJ . I found that the issue was due to another plugin on the site. – Rowayda Khayri Commented Sep 12, 2019 at 19:35
 |  Show 1 more comment

1 Answer 1

Reset to default 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 ).

本文标签: Wordpress action adminpost for nonadmin authenticated usersForm submission