TransWikia.com

How to send email when my plugin is activated?

WordPress Development Asked on October 30, 2021

I’m creating and developing a new plugin for some of my private clients and I do want to see if my clients are sharing my plugin with other people (I asked them not to do that).

How can I do something in my plugin every time plugin is activated, his website sends an email from ‘[email protected]’ to one or more emails I want ‘[email protected]’ and ‘[email protected]’.

By the way, does this include if they update the plugin? Or I just would be able to see who activates only? And does it include the current activated plugin?

2 Answers

So there are two parts to your question:

  1. How to run code when your plugin is activated
  2. How to send an email

For item 1 I searched for 'wordpress plugin activation hook' and found https://developer.wordpress.org/plugins/plugin-basics/activation-deactivation-hooks/ which describes this hook so you can call your own fucntion when a plugin is activated:

register_activation_hook( __FILE__, 'pluginprefix_function_to_run' );

It is documented here: https://developer.wordpress.org/reference/functions/register_activation_hook/

For item 2, there are many examples of online of how to send email from PHP and/or inside the Wordpress infrastructure. As you are using this for a notification, I would suggest that you think about sending this notification with a very simple REST API style call instead. E.g. you could make a URL like yoursite.com/plugin/ping and your plugin could call that URL whenever it's activated, attaching extra information as required in e.g. the request parameters.

Note that however you send the notification, collecting identifiable information about people without their consent is immoral and also illegal in e.g. the EU under GDPR so you need to be very careful with what information you collect. You may want to consider licensing your software and having users agree to a license rather than what you're doing, however that subject is way off topic here.

Answered by mozboz on October 30, 2021

I offer you an idea that I have already used something like this. I wanted to know the sites that my plugin uses, I created a cron job allow me to send me an email every week :

add_action('init', function() {
 $time = wp_next_scheduled('dro_cron_hook');
 wp_unschedule_event($time, 'dro_cron_hook');

 if (!wp_next_scheduled('dro_cron_hook')) {
    wp_schedule_event(time(), 'everyweek', 'dro_cron_hook');
 }
});

add_filter('cron_schedules', function($schedules) {
$schedules['everyweek'] = array(
    'interval' => 604800
 );
 return $schedules;
});

add_action('dro_cron_hook', function() {

 // Here you can send the email using wp_mail() and some additional data
});

Answered by Younes.D on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP