Tag Archives: wordpress

Blog, Easy Digital Downloads, Moneybird

Easy Digital Downloads MoneyBord 1.0.7

In the latest release of Easy Digital Downloads MoneyBird, EDD MoneyBird in short, we have added several cool new features: Support for periods in MoneyBird If you use EDD recurring, the synchonised invoices will now have the corresponding periods added. Support for the EDD VAT EU: VAT number and VAT company will be synchronised as…

Continue reading

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);
Blog

Exclude pages from caching based on post type

To improve site speed for a website I often use WP Rocket: I think it’s one of the fastest caching plugins for WordPress. When a page is being cached, it’s actually saved in a separate folder as a html file. This way, the website doesn’t have to create the page from the php code: there’s a ready made file that can be served immediately. This is way faster then when the server needs to parse the entire code (of course this depends on your server speed as well).

In some cases it is not convenient to cache every page straight from the cache. For example, if you have a webshop with a sidebar with cart, where sometimes a product is in the cart, but sometimes it isn’t. You don’t want the product to be always in the cart, even if the user didn’t add it yet… This is what I encountered with an Easy Digital Downloads webshop.

Of course you can disable caching for those pages. That’s what I did first: I added the DONOTCACHE constant to the downloads template. But when you do that, you also lose the other features of WP Rocket, like the javascript minification etc. As a result, users who have visited the homepage first, have the minified js files in the cache, so the browser will then download the unminified js files. This is not optimal. I wanted to keep those features, without the page being cached.

To achieve this I’ve contact WP Rocket support, who were very helpful. In the end we created a solution where the page cache is not used for the post type ‘download’. In WooCommerce you could use post type ‘product’ to achieve the same thing.

/* 

  don't create page cache on downloads pages. 

*/

add_action('wp', 'rsssl_wprocket_no_page_cache');

function rsssl_wprocket_no_page_cache(){
  global $post;

  if ($post && ( get_post_type($post)=='download' ) ) {

     add_filter('do_rocket_generate_caching_files','__return_false');

  }

}
Ultimate Member

Automatically send private messages in Ultimate Member

For a project where messages had to be sent automatically to the users without additional email being sent, I’ve chosen to use private messages for this purpose.

In this project when a user uploads a new file, a private message should be sent to another user. This way there is not a flood of annoying emails. Ultimate Member will send an email if the receiving user does not log in to check his private messages. This email will normally be sent once a day.

In the code where the update is received, I add this function:

rsp_send_private_message($to, $from, $message);

 /** 
  * Send a private message from a user to a user 
  * 
  * @since   
  * @param  int $to   User id the message is directed to
  * @param  int $from User id the message should come from
  * @param  string $message The text message
  * @return bool             void 
*/
function rsp_send_private_message($to, $from, $message){
  global $ultimatemember, $um_messaging;
  // Create conversation and add message
  $_POST['content']  = "Automatically generated message: " . $message;
  $conversation_id = $um_messaging->api->create_conversation( $to, $from );
  //$output['messages'] = $um_messaging->api->get_conversation($to, $from, $conversation_id );
  $_POST['content'] = "";
  do_action('um_after_new_message', $to, $from, $conversation_id );
}
×