• Resolved divixmu

    (@divixmu)


    Hello, how can I change the target page time to the current time in time zone.
    As I see both the source and the landing page are the same date and time.
    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author ServerPress

    (@serverpress)

    Hello divixmu,

    Thank you for your interest in WPSiteSync.

    There is a way you can do this. An action hook is called after WPSiteSync finishing updating a post on the Target. You can use this hook to modify the date:

    add_action(‘spectrom_sync_push_content’, ‘callback_function’, 10, 3);
    /*
    * @param $target_post_id int The Target’s post ID
    * @param $post_data Array The post data being updated
    * @param $response SyncApiResponse The response object for the current API call
    */
    function callback_function($target_post_id, $post_data, SyncApiResponse $response)
    {
    $new_post_date = strtotime($post_data[‘post_date’]);
    $new_post_date += 2 * 60 * 60;
    wp_update_post(array(
    ‘ID’ => $target_post_id,
    ‘post_date’ => date(‘Y-m-d H:i:s’, $new_post_date)
    ));
    }

    This example will add 2 hours to the post_date column, adjusting for instance from GMT-8 to GMT-6. If you need a different adjustment, you can add or subtract any other amount that you need.

    One thing to note: This is something that we will be handling automatically in the future. The Timezone setting on the Source site will be compared to the Timezone setting on the Target site and used to adjust the post_date and post_modified columns automatically. When this feature is enabled, your callback function will no longer be needed.

    All the best,
    The ServerPress Team

    Thread Starter divixmu

    (@divixmu)

    Hello, I have tried your code to produce the result: Published
    1970/01/01
    I don’t know where it is wrong.

    add_action(‘spectrom_sync_push_content’, ‘callback_function’, 10, 3);
    function callback_function($target_post_id, $post_data, $response)
    {
    $new_post_date = strtotime($post_data[‘post_date’]);
    $new_post_date += 2 * 60 * 60;
    wp_update_post(array(
    ‘ID’ => $target_post_id,
    ‘post_date’ => date(‘Y-m-d H:i:s’, $new_post_date)
    ));
    }
    Plugin Author ServerPress

    (@serverpress)

    Hi divixmu,

    I think I see the problem. Please submit a support request ticket to our Support team here: https://serverpress.com/contact/

    We check this several times a day and can respond to you faster there. In the mean time, we’ll work on some changes to the code above.

    All the best,
    The ServerPress Team

    Thread Starter divixmu

    (@divixmu)

    I sent a ticket but no one seems to answer.

    I am trying to change the publication date of the article and the featured image, but it seems to be unsuccessful ..

    I tried with the code below, it changed the post date on the target page, but there was a problem. when I resynced though without changing anything it recreated the date of the post.

    add_action('spectrom_sync_push_content', 'callback_function', 10, 3);
    function callback_function($target_post_id, $post_data, $response)
    {
    $time = current_time('mysql');
    wp_update_post(array(
    'ID' => $target_post_id,
    'post_date'     => $time,
    'post_date_gmt' => get_gmt_from_date( $time )
    ));
    }

    I also want the image directory to be changed to the current time zone when posts are synced.

    Thanks

    Plugin Author ServerPress

    (@serverpress)

    Hello divixmu,

    For some reason our support form didn’t forward the email to our support ticket system. We’ve found your support request and are replying to that email.

    The code you’re showing above would reset the post_date to the current date/time, each time you Push the content. And it sounds like that’s what you are describing.

    For others that may be interested, the following code will modify the post_date, adding for example, two hours.

    add_action( ‘spectrom_sync_push_content’, ‘sync_update_post_date’, 10, 3 );
    function sync_update_post_date( $target_post_id, $post_data, $response )
    {
    $new_post_date = DateTime::createFromFormat( ‘Y-m-d H:i:s’, $post_data[‘post_date’] );
    // adjust DateInterval as necessary to change the post date
    $new_post_date->add( new DateInterval(‘PT2H’) ); // T=time H=hours- add two hours to post_date
    wp_update_post( array(
    ‘ID’ => $target_post_id,
    ‘post_date’ => $new_post_date->format( ‘Y-m-d H:i:s’ )
    ));
    }

    All the best,
    The ServerPress Team

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Change target post time’ is closed to new replies.