Created
November 12, 2025 22:44
-
-
Save MikeGillihan/f01cab437baeee95938a82f47168808c to your computer and use it in GitHub Desktop.
cl_date shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace CLToolkit; | |
| use CLToolkit\Traits\Singleton; | |
| use DateTime; | |
| use DateTimeZone; | |
| use Exception; | |
| class Shortcodes { | |
| use Singleton; | |
| public function init() { | |
| add_shortcode( 'cl_date', [ $this, 'cl_date_shortcode_handler' ] ); | |
| } | |
| /** | |
| * Shortcode to output a date | |
| * | |
| * @param $atts | |
| * | |
| * @return string | |
| * @throws Exception | |
| */ | |
| public function cl_date_shortcode_handler( $atts ) { | |
| if ( empty( $atts ) ) { | |
| $atts = shortcode_atts( array( | |
| 'format' => 'Y', | |
| ), $atts ); | |
| } | |
| $date = gmdate( $atts['format'] ); | |
| $tz = get_option( 'timezone_string' ); | |
| if ( ! empty( $tz ) ) { | |
| $tzdate = new DateTime( gmdate( 'Y-m-d H:i:s' ), new DateTimeZone( 'UTC' ) ); | |
| $tzdate->setTimezone( new DateTimeZone( $tz ) ); | |
| $date = $tzdate->format( $atts['format'] ); | |
| } | |
| return $date; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment