I can't figure out why get_users()
works on the frontend but not on the admin backend. I'm sure this was working at some point.
My code:
function add_new_user_menu_item() {
remove_submenu_page('users.php', 'user-new.php');
$submenu_title = __( 'Add new', 'myplugin' );
add_submenu_page(
'users.php',
__( 'Add user', 'myplugin' ),
$submenu_title,
'create_users',
'new-user',
'manage_new_user_submit'
);
} add_action( 'admin_menu', 'add_new_user_menu_item' );
function manage_new_user_submit() {
if ( isset( $_POST ) && isset( $_POST['action'] ) ) {
if ( $_POST['action'] == 'newuser' && wp_verify_nonce( $_POST['_wpnonce_add-user'], 'add-user' ) ) {
session_start();
$errors = array();
$is_registered = get_users( array(
'meta_key' => 'my_meta_key',
'meta_value' => sanitize_text_field($_POST['my_meta_value']),
'fields' => array( 'id', 'user_email' ),
'role' => 'customer',
));
// $is_registered returns an array()
error_log("Register: " . json_encode($is_registered));
}
....
}
}
It returns an empty array. If I run the function get_users()
on the frontend on template_redirect
action hook I get the user correctly.
Could it be a permission issue? No, right? It runs on the frontend. Am I calling it too early? Don't think so.
Edit: Details added
I can't figure out why get_users()
works on the frontend but not on the admin backend. I'm sure this was working at some point.
My code:
function add_new_user_menu_item() {
remove_submenu_page('users.php', 'user-new.php');
$submenu_title = __( 'Add new', 'myplugin' );
add_submenu_page(
'users.php',
__( 'Add user', 'myplugin' ),
$submenu_title,
'create_users',
'new-user',
'manage_new_user_submit'
);
} add_action( 'admin_menu', 'add_new_user_menu_item' );
function manage_new_user_submit() {
if ( isset( $_POST ) && isset( $_POST['action'] ) ) {
if ( $_POST['action'] == 'newuser' && wp_verify_nonce( $_POST['_wpnonce_add-user'], 'add-user' ) ) {
session_start();
$errors = array();
$is_registered = get_users( array(
'meta_key' => 'my_meta_key',
'meta_value' => sanitize_text_field($_POST['my_meta_value']),
'fields' => array( 'id', 'user_email' ),
'role' => 'customer',
));
// $is_registered returns an array()
error_log("Register: " . json_encode($is_registered));
}
....
}
}
It returns an empty array. If I run the function get_users()
on the frontend on template_redirect
action hook I get the user correctly.
Could it be a permission issue? No, right? It runs on the frontend. Am I calling it too early? Don't think so.
Edit: Details added
Share Improve this question edited Sep 14, 2019 at 0:55 Caio Mar asked Sep 13, 2019 at 23:25 Caio MarCaio Mar 4344 silver badges16 bronze badges 3- Should we assume that "backend" refers to "Admin area"? Also .. perhaps you could include more of your code, so we can get some actual context. You are asking us to help troubleshoot a fairly complex situation, with very little to go on. – Mike Baxter Commented Sep 14, 2019 at 0:32
- Thanks man, you're right. I added more info and tried to elaborate it better. – Caio Mar Commented Sep 14, 2019 at 0:57
- I have a similar issue. get_users with meta_key works, but adding 'role__in' does nothing. It seems the mechanics for roles is not registered at the time of save_post. – ggedde Commented May 1, 2021 at 0:36
1 Answer
Reset to default 0Perhaps user.php has not yet been loaded. get_users() is a wrapper for WP_User_Query, which is defined in wp-includes/user.php. Hooks indicate template_redirect is after wp, after users are registered.
Perhaps you could try conditionally hooking 'template_redirect' on the front-end and 'user_admin_menu' on the back-end.
if (is_admin()){
add_action('user_admin_menu','manage_new_user_submit');
} else {
add_action('template_redirect','manage_new_user_submit');
}
Also, you might remove the trailing comma in that get_users() command. That can sometimes result in null.