$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Sync user meta fields using Wp cron job|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Sync user meta fields using Wp cron job

matteradmin8PV0评论

I'm searching about using wp cron job to sync two user meta fields monthly. I'm using a very awesome plugin called "SyncFields" and it works fine automaticaly, But I'm looking for sync it every month.

I'm searching about using wp cron job to sync two user meta fields monthly. I'm using a very awesome plugin called "SyncFields" and it works fine automaticaly, But I'm looking for sync it every month.

Share Improve this question edited Nov 20, 2018 at 21:50 Gamal Elwazeery asked Nov 20, 2018 at 21:36 Gamal ElwazeeryGamal Elwazeery 259 bronze badges 5
  • Hi. What exactly do you mean by "sync two user meta fields monthly"? Could you elaborate a little bit more on that? Some examples would be great to, I guess :) – Krzysiek Dróżdż Commented Nov 20, 2018 at 21:40
  • What I mean is to sync two user fields like "firstname" and "billing_firstname" but using cron job to sync it monthly – Gamal Elwazeery Commented Nov 20, 2018 at 21:45
  • Do you want to use the plugin or some specific code to perform the monthly sync? If so, it's easy enough but you will have to extend the wp_cron to handle a 30 day sync. Getting the wp_cron to recognize a calendar month requires a little finesse but it can certainly handle a 30 day repeat cycle. – jdp Commented Nov 20, 2018 at 21:58
  • @jdp I tested the plugin but it triggers automaticly after any edit on the fields , So I'm looking for a code to do this in cron every 30 days – Gamal Elwazeery Commented Nov 20, 2018 at 22:00
  • Any ideas for it? – Gamal Elwazeery Commented Nov 20, 2018 at 22:21
Add a comment  | 

1 Answer 1

Reset to default 0

If you want to remove the functionality of the original plugin, try adding this to your theme's functions.php file

function add_thirty_day_cron($schedules) {
  $schedules['thirtydays'] = array( 'interval' => 30 * DAY_IN_SECONDS, 'display' => __( 'Every 30 days') );
  return $schedules;
}
add_filter('cron_schedules','add_thirty_day_cron');//adds 30 day interval
add_action('do_meta_sync', 'my_meta_sync');//hook for cron
function my_meta_sync() {
  if (function_exists('msf_action_callback')) msf_action_callback();
}
function modify_syncfields_sync() {
  remove_action('edit_user_profile_update', 'msf_action_callback');
  remove_action('profile_update', 'msf_action_callback');
}
add_action('plugins_loaded','modify_syncfields_sync');//removes plugin actions
add_action('wp_loaded','add_my_sync_cron');//schedules cron
function add_my_sync_cron() {
  if (!wp_next_scheduled('do_meta_sync')) wp_schedule_event(time(), 'thirtydays', 'do_meta_sync');
}

If you don't want to use the plugin anymore, here are two options. The first will run every thirty days starting on the first day of next month.

function my_meta_sync() {
    $users = get_users();
    foreach ($users as $user) {
        $meta = get_user_meta( $user->ID );
        if ($meta['firstname'][0] != $meta['billing_firstname'][0]) update_user_meta( $user->ID, $meta['billing_firstname'][0], $meta['firstname'][0] );
    }
}
//use this code for 30 day intervals
add_filter('cron_schedules','add_thirty_day_cron');//adds 30 day interval
function add_thirty_day_cron($schedules) {
  $schedules['thirtydays'] = array( 'interval' => 30 * DAY_IN_SECONDS, 'display' => __( 'Every 30 days') );
  return $schedules;
}
add_action('do_thirty_day_meta_sync', 'my_meta_sync');//hook for cron
add_action('wp_loaded','add_my_thirty_day_sync_cron');//schedules cron
function add_my_thirty_day_sync_cron() {
  if (!wp_next_scheduled('do_thirty_day_meta_sync')) {
      $timestamp = date_create('first day of next month')->format('U');
      wp_schedule_event($timestamp, 'thirtydays', 'do_thirty_day_meta_sync');

  }
}
//end

To run the sync on the first day of the month, you'll need to schedule a daily cron.

function my_meta_sync() {
    $users = get_users();
    foreach ($users as $user) {
        $meta = get_user_meta( $user->ID );
        if ($meta['firstname'][0] != $meta['billing_firstname'][0]) update_user_meta( $user->ID, $meta['billing_firstname'][0], $meta['firstname'][0] );
    }
}
function my_monthly_meta_sync() {
    if (date('d') == '01') my_meta_sync();
}
function add_daily_tasks() {
    if (!wp_next_scheduled('my_daily_cron')) wp_schedule_event(time(), 'daily', 'my_daily_cron');
}
add_action('wp_loaded','add_daily_tasks');//schedules cron
function my_daily_tasks(){
    my_monthly_meta_sync();
}
add_action('my_daily_cron','my_daily_tasks');
//end

I created a generic daily cron task because once you get used to running cron style tasks, especially daily ones, you'll want to do more. Going forward, all you have to do is call your new functions inside the my_daily_tasks() and you won't have to create more cron hooks as long as they are independent of time of day.

I would also recommend installing a cron plugin (I use WP Crontrol) to see what's going on in your cron jobs. You can also create/delete crons using the plugin rather than using wp_schedule_event() like I did in the hard code.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far