BuddyDev

Search

[Resolved] Send email to user that has been inactive for 2 months

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38234

    Hello to the team 🙂

    I am trying to make use of $last_activity = bp_get_user_last_activity($user_id); to create a reminder email whenever a user has not been active for a period of 2 months. But i am not sure how to create a function like that.

    How could i create an if statement that could check that the $last_activity is more that 2 months and then fire an email to that user?

    function email_if_user_inactive( $user_id ) {
        $last_activity = strtotime( bp_get_user_last_activity( $user_id ) );
     
        if ( empty( $last_activity ) ) {
            return;
        }
     
        if ( $last_activity  ) { // if last activity was before 2 months fire an email to that user
            
            wp_mail( $email_to, $email_subject, $email_message );
        }
    }

    And how could i make a check (maybe some action)to all the users in db so that i can set this function?

    Thank you very much for your time and support.

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #38256

    im really interested in this

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #38264

    Hi,
    Thank you for the question. This is a good requirement but we are unable to assist you here(This iwll need significant time and code)

    Here is what you can look for

    1. setup a cron job daily checking of inactive users
    2. Use a background queue to send emails to the users. You may want to check WP Background processing package.

    These are significant steps and the code is beyond what we can offer here.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38287

    Hello @Brajesh and thank you very much for your help.

    After some digging that i have done, i have created a small plugin to do the work.
    I am just stucked in some points and not sure how to proceed.

    I would like to share what i did, since i sincerely believe that you have a good community + a very supportive forum and i like sharing.

    Please have a short look on it and let me know if you have any suggestions-corrections .
    https://pastebin.com/Unz9kXYi

    (I have made it as a plugin so that if i want to disalbe the emails, to just disable the plugin.)

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #38507

    I found a similar plugin can you take a look at it https://wordpress.org/plugins/come-back/

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38529

    thank you @Tosin for your info. But this plugin is not actually what i am looking for.But i will have a look on it.

    @Brajesh could you please take a look on the query below and give me a hint what am i doing wrong :

    
     // Get the contributors and administrators who haven't logged in for 60 days
        $args = array( 
        'role__in' => array( 'contributor', 'administrator' ),
    	'meta_key'     => 'last_activity', // this is a meta_key storing the last time the user was logged in by buddypress in 'Y-m-d H:i:s' format
    	'meta_query' => array( 
            array(
                'key' => 'last_activity',
                'value' => date('Y-m-d H:i:s', strtotime( "-60 days" )), // what am i doing wrong? 
                'compare' => '<',
                'type' => 'DATE' // Let WordPress know we're working with date
                )
            )
        );
    
        $remind_users = new WP_User_Query( $args ); 
    

    I am trying to get the inactive users for the last 60 days, but for some reason it doesn’t give me the correct result. Any help please? @Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38540

    Hello Lefteris,

    Try using the following code and let me know if it works or not.

    
    
    function buddydev_get_last_days_active_users() {
    
    	global $wpdb;
    	$bp = buddypress();
    
    	$user_ids = $wpdb->get_col(
    		$wpdb->prepare(
    			"SELECT user_id FROM {$bp->members->table_name_last_activity} 
    			WHERE component = %s AND type = 'last_activity' AND 
    			date_recorded > current_date - interval 7 day",
    			$bp->members->id,
    		)
    	);
    
    	// No user found.
    	if ( empty( $user_ids ) ) {
    		return;
    	}
    
    	$users = array();
    	foreach ( $user_ids as $user_id ) {
    
    		$user = get_user_by( 'id', $user_id );
    
    		if ( $user && array_intersect( $user->roles, array( 'contributor', 'administrator' ) ) ) {
    			$users[] = $user;
    		}
    	}
    
    	return $users;
    }
    
    

    Modify it as you need.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38558

    Thank you so much @Ravi , your help has been magnificent!!

    Could you just check what i did from point of view of syntax? https://pastebin.com/EfASFZNa

    Your code works perfectly fine. Just hade to change date_recorded < current_date - interval 2 month for getting all users who haven’t logged in for the last 2 months.

    Is the `if ( empty( $user_ids ) ) {
    return;
    } correct or should i just make anif ( ! empty( $user_ids ) )in theforeach ( $user_ids as $user_id )` loop?

    Thank you again for you time. I am grateful for your support.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38562

    Hello Lefteris,

    Instead of ‘date_recorded < current_date – interval 2 month’ use this ‘date_recorded > current_date – interval 60 day’. Give it a show this way and let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #38618

    hello @Ravi, after many tests it works perfectly fine with date_recorded < current_date - interval 60 day for getting the users that haven’t been active for more than 2 months.

    Is the if ( empty( $user_ids ) ) { return; } correct as it should be in the code? https://pastebin.com/EfASFZNa

    or should i just make an if ( ! empty( $user_ids ) ) in the foreach ( $user_ids as $user_id ) loop?

    Thank you for your help

The topic ‘ [Resolved] Send email to user that has been inactive for 2 months’ is closed to new replies.

This topic is: resolved