I have a custom filter called user type in the admin user dashboard and I have already worked it out to display them in the user admin dashboard. But I don't know how to call and save them in an array so that I can get the user_ids and perform another action.
My other action would be to get all the meta keys of a certain user and I would like to export them into an excel file. But before I can do that I should have an option to filter them or just export all the users, which I have already performed it.
function add_course_section_filter() {
if ( isset( $_GET[ 'user_type' ]) ) {
$user_type = $_GET[ 'user_type' ];
} else {
$user_type = '';
}
echo ' <select name="user_type[]" style="float:none;"><option value="">Choose User Type...</option>';
if($user_type[0] == 'designer'){
$selected_designer = 'selected="selected"';
}
if($user_type[0] == 'client'){
$selected_client = 'selected="selected"';
}
echo '<option value="designer" ' . $selected_designer . '>Builder/Designer</option>';
echo '<option value="client" ' . $selected_client . '>Client</option>';
echo '</select>';
echo '<input type="submit" class="button" value="Filter">';
}
add_action( 'restrict_manage_users', 'add_course_section_filter' );
function filter_users_by_course_section( $query ) {
global $pagenow;
if ( is_admin() && 'users.php' == $pagenow && isset( $_GET[ 'user_type' ] )) {
$user_type = $_GET[ 'user_type' ];
if($user_type[0] != ''){
$meta_query = array(
array(
'key' => '_user_type',
'value' => $user_type[0]
)
);
$query->set( 'meta_key', '_user_type' );
$query->set( 'meta_query', $meta_query );
}else{
}
}
}
add_filter( 'pre_get_users', 'filter_users_by_course_section' );
I have a custom filter called user type in the admin user dashboard and I have already worked it out to display them in the user admin dashboard. But I don't know how to call and save them in an array so that I can get the user_ids and perform another action.
My other action would be to get all the meta keys of a certain user and I would like to export them into an excel file. But before I can do that I should have an option to filter them or just export all the users, which I have already performed it.
function add_course_section_filter() {
if ( isset( $_GET[ 'user_type' ]) ) {
$user_type = $_GET[ 'user_type' ];
} else {
$user_type = '';
}
echo ' <select name="user_type[]" style="float:none;"><option value="">Choose User Type...</option>';
if($user_type[0] == 'designer'){
$selected_designer = 'selected="selected"';
}
if($user_type[0] == 'client'){
$selected_client = 'selected="selected"';
}
echo '<option value="designer" ' . $selected_designer . '>Builder/Designer</option>';
echo '<option value="client" ' . $selected_client . '>Client</option>';
echo '</select>';
echo '<input type="submit" class="button" value="Filter">';
}
add_action( 'restrict_manage_users', 'add_course_section_filter' );
function filter_users_by_course_section( $query ) {
global $pagenow;
if ( is_admin() && 'users.php' == $pagenow && isset( $_GET[ 'user_type' ] )) {
$user_type = $_GET[ 'user_type' ];
if($user_type[0] != ''){
$meta_query = array(
array(
'key' => '_user_type',
'value' => $user_type[0]
)
);
$query->set( 'meta_key', '_user_type' );
$query->set( 'meta_query', $meta_query );
}else{
}
}
}
add_filter( 'pre_get_users', 'filter_users_by_course_section' );
with you code, the user list is filtered by the meta "_user_type".
then you can create a new bulk action to export data of the selected users with that :
const ACTION_EXPORT_CSV = "MyPlugin__exportCSV";
add_filter("bulk_actions-users", function ($actions) {
$actions[ACTION_EXPORT_CSV] = "Export CSV";
return $actions;
});
add_action("load-users.php", function () {
if ( isset($_GET["action"])
&& (ACTION_EXPORT_CSV === $_GET["action"])
) {
// here, identifiers of selected users are in $_GET["users"]
}
});