I'd like to have the contents of a WP page emailed to me regularly, eg once a week.
There are loads of plugins that will email a page when a button is pressed; and loads that will automatically create a newsletter which includes new blog post content, for example.
But I just want the content of the same page each time - the content in question is a bit like a to-do list, and having it emailed to myself once a week (rather than simply when it has been updated) would be very useful. Any ideas?
Kyle answers:
Try this
if (!wp_next_scheduled('my_task_hook')) {
wp_schedule_event( time(), 'weekly', 'my_task_hook' );
}
add_action ( 'my_task_hook', 'my_task_function' );
function my_task_function() {
$my_postid = 1;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
wp_mail( '[email protected]', 'this is the subject', $content );
}
function my_add_weekly( $schedules ) {
// add a 'weekly' schedule to the existing set
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __('Once Weekly')
);
return $schedules;
}
add_filter( 'cron_schedules', 'my_add_weekly' );
User179892 comments:
Hi - thanks. This sounds perfect in principle (I assume I need to add to the functions page?) - but the codex says wp_schedule_event can only have 'hourly','twicedaily' or 'daily'. I guess daily might be OK - but weekly would be even better.
Kyle comments:
I updated my answer with the code to register the 'weekly' schedule
Kyle comments:
Also, note that you might want to change the time() aspect to when you want the emails to go out each week, e.g. if you want them to go out Friday's at 5pm, look up the UTC timestamp for your timezone when that is for this week and then it will go out weekly at that time moving forward
User179892 comments:
Just for the purposes of testing this, if I keep it at time(), does that mean it should first trigger immediately, and then a week after now next time? (If so, it doesn't seem to be working.)
Kyle comments:
Yes, but you can use a plugin like crontrol https://wordpress.org/plugins/wp-crontrol/ to force it get sent out and make sure your cron settings are working. Not all servers use cron by default, you might have to turn it on. Make sure that in your config file you have
define('DISABLE_WP_CRON', false);
Kyle comments:
Always check spam too to make sure it isn't getting funneled out because of smtp configuration problems
User179485 comments:
I'll have to try crontrol - it's not in spam, and I've added define('DISABLE_WP_CRON', false); but still nothing is happening.
Kyle comments:
Just to confirm, did you edit the email address to your own in the code?
User179485 comments:
Yes, of course! I have installed WP Crontrol. The weekly option is listed in the schedules, but I can't see anything relating to your code in the list of cron events. I'm using a plugin to manage the extra function - I guess I could try editing functions.php directly - but that shouldn't really make a difference. (Thanks for your help, by the way.)
User179485 comments:
Nope, it's not that.
Kyle comments:
Where are you putting the code now if you're not putting it in functions.php?
User179485 comments:
Or maybe it was - it has just worked! Thanks again. I can take it from here.
Kyle comments:
Awesome! Sounds good, let me know if you need any more help
Arnav Joy answers:
Do you like any plugin which sends email when content of the page is changed ??
If yes then I will tweek it to send you mail every one week automatically.