admin管理员组文章数量:1321440
My workaround at the moment, within a shortcode, is to just copy the WP native code to get the Timezone within my (namespaced) class:
new \DateTimeZone( $this->wp_timezone_string() ));
private function wp_timezone_string() {
$timezone_string = get_option( 'timezone_string' );
if ( $timezone_string ) {
return $timezone_string;
}
$offset = (float) get_option( 'gmt_offset' );
$hours = (int) $offset;
$minutes = ( $offset - $hours );
$sign = ( $offset < 0 ) ? '-' : '+';
$abs_hour = abs( $hours );
$abs_mins = abs( $minutes * 60 );
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
return $tz_offset;
}
But I'm wondering why I can't just call make a call to \wp_timezone()
(escaped for namespace).
Is it because the file that contains that class hasn't loaded yet?
My workaround at the moment, within a shortcode, is to just copy the WP native code to get the Timezone within my (namespaced) class:
new \DateTimeZone( $this->wp_timezone_string() ));
private function wp_timezone_string() {
$timezone_string = get_option( 'timezone_string' );
if ( $timezone_string ) {
return $timezone_string;
}
$offset = (float) get_option( 'gmt_offset' );
$hours = (int) $offset;
$minutes = ( $offset - $hours );
$sign = ( $offset < 0 ) ? '-' : '+';
$abs_hour = abs( $hours );
$abs_mins = abs( $minutes * 60 );
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
return $tz_offset;
}
But I'm wondering why I can't just call make a call to \wp_timezone()
(escaped for namespace).
Is it because the file that contains that class hasn't loaded yet?
Share Improve this question asked Sep 23, 2020 at 17:53 MikeiLLMikeiLL 5791 gold badge8 silver badges22 bronze badges 6- Where/when are you trying to use it? Is this inside a theme? – Tom J Nowell ♦ Commented Sep 23, 2020 at 18:27
- No it's within a plugin, called by a shortcode. – MikeiLL Commented Sep 23, 2020 at 19:21
- and you're using the latest WordPress? – Tom J Nowell ♦ Commented Sep 23, 2020 at 21:57
- 1 There's the rub! I was developing in v5.2 and these functions are since v5.3. – MikeiLL Commented Sep 23, 2020 at 23:02
- 1 Awesome, can you post it as an answer rather than a comment so I can upvote? – Tom J Nowell ♦ Commented Sep 24, 2020 at 10:00
1 Answer
Reset to default 1Thank you Tom J Nowell for the insightful comment above.
Turns out that wp_timezone is a relatively recent addition to Wordpress, and didn't exist in the version of WP I was developing in. It was added, as the Changelog says, in v5.3
. I was using v5.2
.
The function itself:
function wp_timezone() {
return new DateTimeZone( wp_timezone_string() );
}
Simpy wraps the result of wp_timezone_string
in a native php DateTimeZone
object. The wp_timezone_string
was also added in v5.3
.
Since this is a brand new plugin, I'm not going to support older version of Wordpress. For pragmatic reasons, and in solidarity with the movement to encourage users to upgrade WP environments, I have also opted to not support versions of php (like v5.6
) that are past their End of Life.
本文标签: timezonesGetting wptimezone undefined within a namespace
版权声明:本文标题:timezones - Getting wp_timezone undefined within a namespace 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742102118a2420848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论