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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
});
Method 2
So there are two parts to your question:
- How to run code when your plugin is activated
- 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0