1

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.

  • 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ż Nov 20 '18 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 Nov 20 '18 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 Nov 20 '18 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 Nov 20 '18 at 22:00
  • Any ideas for it? – Gamal Elwazeery Nov 20 '18 at 22:21
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.

  • Thank you, and if I want to add a seprate new code without using the plugin? Just for two fields – Gamal Elwazeery Nov 21 '18 at 2:38
  • Well...the plugin creates a separate table with the custom meta fields. The plugin also has a form to add the custom user meta fields. The form can be replicated by creating form control fields for the user meta and hooking into the 'user_new_form' and 'edit_user_profile' action hooks. Saving the user meta can be done by hooking into the 'insert_user_meta' filter hook. You'd also have to create your own sync function. I'm still not clear why you need to sync these two fields. Are you giving the users the ability to edit one of these fields? – jdp Nov 21 '18 at 3:46
  • Yes I'm making a web app and I want the user to edit just one field, and sync it just in the first day of the month because I just receive the order monthly – Gamal Elwazeery Nov 21 '18 at 4:20
  • @GamalElwazeery Which field are they editing? First name? – jdp Nov 21 '18 at 11:28
  • Yes firstname, and the second field is billing_firstname – Gamal Elwazeery Nov 21 '18 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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