Tag Archives: users

Blog

Sending a password reset link for PHP added WordPress users

Sometimes you need to create users in WordPress with PHP code, be it because you’re importing users, or because you want to let one user add other users. When you do this you want the new users to be able to reset their password.

At first I thougth to use a password reset link, to the password reset page in WordPress, but this is not very user friendly:

  1. The user has to enter his username or email address
  2. The user will receive a resetmail with a link.
  3. After clicking it, the user can reset the password.

It would be much better if we could directly send the resetlink that leads to the third step. To do this we need to create a password reset key, which is only temporarily valid.

The code below generates such an email.

function dtc_send_password_reset_mail($user_id){

    $user = get_user_by('id', $user_id);
    $firstname = $user->first_name;
    $email = $user->user_email;
    $adt_rp_key = get_password_reset_key( $user );
    $user_login = $user->user_login;
    $rp_link = '<a href="' . wp_login_url()."/resetpass/?key=$adt_rp_key&login=" . rawurlencode($user_login) . '">' . wp_login_url()."/resetpass/?key=$adt_rp_key&login=" . rawurlencode($user_login) . '</a>';

    if ($firstname == "") $firstname = "gebruiker";
    $message = "Hi ".$firstname.",<br>";
    $message .= "An account has been created on ".get_bloginfo( 'name' )." for email address ".$email."<br>";
    $message .= "Click here to set the password for your account: <br>";
    $message .= $rp_link.'<br>';

    //deze functie moet je zelf nog toevoegen. 
   $subject = __("Your account on ".get_bloginfo( 'name'));
   $headers = array();

   add_filter( 'wp_mail_content_type', function( $content_type ) {return 'text/html';});
   $headers[] = 'From: Your company name <info@your-domain.com>'."\r\n";
   wp_mail( $email, $subject, $message, $headers) === false);

   // Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
   remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
Blog, Ultimate Member

Search users on username, email address, firstname and lastname

If you want to do a search in your WordPress users, or in Ultimate Member members, (which amounts to the same thing), you would think this can be easily done with a meta_query, or with the search query functionality that is built in into WordPress.

This is not the case! When creating some code doing this, I discovered that with a meta_query, a user searched by username doesn’t get found. Looking in the query that is created by WordPress, I noticed that WordPress only searches in the wp_user_meta table. The username is part of the wp_users table.

On the other hand, if you use the “search” query, WordPress only searches in the users table, so won’t find users by firstname or lastname.

To solve it, you have to combine the two. In the example below, the totalusers variable contains the list of users that matches the input $str on either username, user_nicename, user_email, firstname, or lastname. You can extend it at will of course.

//search usertable
$wp_user_query = new WP_User_Query(
  array(
    'search' => "*{$str}*",
    'search_columns' => array(
    'user_login',
    'user_nicename',
    'user_email',
  ),

) );
 $users = $wp_user_query->get_results();

//search usermeta
 $wp_user_query2 = new WP_User_Query(
  array(
    'meta_query' => array(
    'relation' => 'OR',
      array(
        'key' => 'first_name',
        'value' => $str,
        'compare' => 'LIKE'
      ),
    array(
        'key' => 'last_name',
        'value' => $str,
        'compare' => 'LIKE'
      )
    )
  )
 );

$users2 = $wp_user_query2->get_results();

$totalusers_dup = array_merge($users,$users2);

$totalusers = array_unique($totalusers_dup, SORT_REGULAR);
×