I want to email the new page password to somebody every month. I see no reason why this cannot be adapted.

First I guess I need to change this:

add_filter( 'cron_schedules', function( $schedules )
{
    // Our custom cron interval:
    $schedules['every_2nd_day'] = array(
        'interval'  => 2 * DAY_IN_SECONDS,
        'display'   => 'Once every second day'
    );

to

add_filter( 'cron_schedules', function( $schedules )
{
    // Our custom cron interval:
    $schedules['every_month'] = array(
        'interval'  => 1 * MONTH_IN_SECONDS,
        'display'   => 'Once every Month'
    );

And change the code again later where ‘every_2nd_day’ is referenced. Then how do I a) set up an automated email, and b) package the code as a plugin and install it?

Is that all I need do. I assume this script is still secure, and valid for current WordPress?

1 Answer
1

First of all, you should wrap your cron into a method:

function cronjob_once_every_month() {
    // Our custom cron interval:
    $schedules['every_month'] = array(
        'interval'  => 1 * MONTH_IN_SECONDS,
        'display'   => 'Once every Month'
    );
}

a) set up an automated email

Then you should schedule an action:

if ( ! wp_next_scheduled( 'cronjob_once_every_month' ) ) {
    wp_schedule_event( time(), 'email_password_every_month', 'cronjob_once_every_month' );
}

Finally, you should hook into an action to fire the cron:

add_action( 'cronjob_once_every_month', 'email_password_every_month' );
function email_password_every_month() {
    wp_mail( $to, $subject, $message, $headers, $attachments );
}

Tip: you should setup SMTP because wp_mail() does not guarantee the email was received by the user.

b) package the code as a plugin and install it?

Please, read the official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *