Pregunta

i was working in a wordpress registration plugin. i stucked in expiry of the user. actually i want to expire the member after one year of his/her registration. and i want to notify them via email before 1 month of their expiry. i am using add_action('init','my function name') to check how many of the user is going to expire after a month and also to send the mail. bt this action hook will run every time a user visits the site which will make my site too slow to load everytime a user will visit. so i want something dat will make this code run once in a day. e.g. when the first user visit the site this code will run and for the whole remaining day this code will not be invoke no matter how many user will visit the website.

¿Fue útil?

Solución

Wordpress has a built-in function/API that just do exactly what you want - doing something every day/hour/any interval you specify.

http://codex.wordpress.org/Function_Reference/wp_schedule_event

Taken shamelessly from the above page

add_action( 'wp', 'prefix_setup_schedule' );
/**
 * On an early action hook, check if the hook is scheduled - if not, schedule it.
 */
function prefix_setup_schedule() {
    if ( ! wp_next_scheduled( 'prefix_daily_event' ) ) {
        wp_schedule_event( time(), 'daily', 'prefix_daily_event');
    }
}


add_action( 'prefix_daily_event', 'prefix_do_this_daily' );
/**
 * On the scheduled action hook, run a function.
 */
function prefix_do_this_daily() {
    // check every user and see if their account is expiring, if yes, send your email.
}

prefix_ is presumably to ensure there will be no collision with other plugins, so I suggest you to change this to something unique.

See http://wp.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress/ if you want to know more.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top