• Resolved eye8

    (@eye8)


    I encountered this problem and have struggled with it for hours without luck. The context is we use our custom script to allow users to create multisite. I have specified the option ‘timezone_string’=> ‘America/New_York’ while calling the function wpmu_create_blog(). The problem is the blog is still created under UTC-0 (as shown in mysql db) and so is the dummy post, dummy page, and dummy comment. However the ‘timezone_string’ setting is applied after the dummy contents are created. So if our user publishes a post immediately after setting up the multisite, their post will become the first post but the dummy post becomes the second since New York is behind UTC-4.

    This looks like a bug in the wpmu_create_blog() function. Shouldn’t blog options be applied before dummy contents are injected? But no matter if this is a bug or not, how can I fix this error? I have made sure the main blog uses the correct timezone, and the Apache time is correct as well. What am I missing. I also tried update_site_option() before calling wpmu_create_blog(), which didn’t help.

    You help is very appreciated!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Thread Starter eye8

    (@eye8)

    Thanks for the reply Ipstenu. I am aware of similar plugins to achieve this purpose in the WordPress plugins. But in our project we don’t want to use 3rd-party plugins unless there’s no other choices.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    *points to plugin*

    Either use it, or reverse engineer it (it’s open source, you seem code savvy) to see how it does it.

    But at the very least, I would use it and see if IT works ??

    Thread Starter eye8

    (@eye8)

    Thanks a lot. I guess that’s what I might end up doing. Or, I could also update the creation time of the dummy posts in the site theme once the blog is created.
    However, I still hope the WordPress team could improve the wpmu_create_blog() function so that users won’t have to take the trouble. Otherwise what is the purpose of having the “meta” param in the argument?

    Have a look at upgrade.php. The function wp_install_defaults($user_id) is pluggable.

    Sooooo ….

    The 2 minute fix for me was to copy/paste the entire function verbatim from upgrade.php into any .php file in /mu-plugins/

    Then I tinkered with the 2 lines setting the first post timestamp.

    I got the current tz time from blog # 1:

    ...
    	// First post
    //	$now = date('Y-m-d H:i:s');
    //	$now_gmt = gmdate('Y-m-d H:i:s');
    switch_to_blog('1');
    	$now = current_time('mysql');
    	$now_gmt = current_time('mysql',0);
    restore_current_blog();
    ...

    Or

    If it really that important that the “Hello World” post is more than 24h in the past, make it a good day in history.

    // First post
    //	$now = date('Y-m-d H:i:s');
    //	$now_gmt = gmdate('Y-m-d H:i:s');
    	$now = '1969-07-20 20:17:40';
    	$now_gmt = '1969-07-20 20:17:40';

    Done. Now when a first time blogger makes his/her first post in the same hour they activate their blog, the “Hello World” post is in the “correct” past (at least if they are in the same timezone as the main blog).

    Looking back at the OP, you are setting the TZ to New York so you can do that with php just before calling the date, too.

    // First post
    date_default_timezone_set('America/New_York');
    	$now = date('Y-m-d H:i:s');
    	$now_gmt = gmdate('Y-m-d H:i:s');

    Something else to try in mu-plugins:

    <?php
    add_action('populate_options', create_function('$a', 'return date_default_timezone_set("America/New_York");'));
    ?>

    Thread Starter eye8

    (@eye8)

    Hi Mr. Sader, this actually works for me! Although I might have to update the customized wp_install_defaults() function every time upgrading the WordPress source code, it does solve the problem and the dummy posts are shown being created at the current time.

    One note to add to your solution. I am using the latest WordPress v. 3.5.1 which has deprecated WPMU Plugins. What I need to do is to create a single-php plugin that holds the customized wp_install_defaults() function and put it in the plugins folder. After activating it across the network the custom function will overwrite the original one in wp-admin-includes/upgrade.php to set the correct timezone for new blogs.

    Note 2: the dummy posts show the *current* time in New York. But in dashboard it also says “published 4 hours ago”. I guess that’s because it first uses the current UTC-0 time then switched to 4 hours later as in New York. Not a problem for me anyway. Topic resolved!

    Many thanks to Mr. Sader and Ipstenu for your help!

    “the latest WordPress v. 3.5.1 which has deprecated WPMU Plugins.” is not the same thing as creating an mu-plugins folder.

    Must-Use plugins are very much still in vogue.

    Thread Starter eye8

    (@eye8)

    Ahhh, thanks for pointing out! I had always taken it for granted “mu-plugins” are for plugins used in multisite. And the history behind the naming is interesting. Will do as you suggested.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Bug? Correct timezone is not applied to dummy post in multisite’ is closed to new replies.