-1

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 2

2

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
});
3
  • Thanks for the idea @Younes.D but it sends around 5-6 emails per minute. I changed the schedules values to biweek but it does not work. Do you have any ideas?
    – Saeed
    Jul 19, 2020 at 6:16
  • There will be several reasons: you receive errors? did you test sending a single email? you know that WordPress crons work differently than Linux crons? the website must be visited for the cron to run
    – Younes.D
    Jul 20, 2020 at 18:01
  • Hello, no errors. Just sending works fine but around 5 emails/minute. Yes I know they work different. I put it in the main file of the plugin and every time I see the site, it sends an email which will be a huge one in a production website.
    – Saeed
    Jul 21, 2020 at 21:47
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.