From 09c9ebb7c77e5dd0405bdf8b5f3453cb3017c253 Mon Sep 17 00:00:00 2001 From: infinitnet Date: Fri, 17 Nov 2023 12:50:44 +0100 Subject: [PATCH] v1.1 release: new features and refactoring --- README.md | 6 +++ wordpress-date-shortcodes.php | 88 ++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index fa535f0..c052822 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,17 @@ WordPress plugin that adds shortcodes for current, published, and last modified ## Purpose The main purpose of this plugin is to dynamically add dates to post titles. The shortcodes also work in meta descriptions (Rank Math, Yoast, SEOPress) and post content. +You can use the `adjust` parameter for `[currentday]` to display the date for tomorrow, yesterday, the day after tomorrow, etc. This can be useful to make discounts always expire "tomorrow", for example: "Hurry up! This discount expires on `[currentmonth] [currentday adjust="+1"]`!" + ## Available Shortcodes +- **[currentday]** - current day +- **[currentday adjust="+1"]** - tomorrow (both `+` and `-` are supported) - **[currentmonth]** - current month - **[currentyear]** - current year +- **[publishedday]** - day the post/page was published - **[publishedmonth]** - month the post/page was published - **[publishedyear]** - year the post/page was published +- **[modifiedday]** - day the post/page was last modified - **[modifiedmonth]** - month the post/page was last modified - **[modifiedyear]** - year the post/page was last modified diff --git a/wordpress-date-shortcodes.php b/wordpress-date-shortcodes.php index 762bf2b..36e7c0b 100644 --- a/wordpress-date-shortcodes.php +++ b/wordpress-date-shortcodes.php @@ -6,60 +6,62 @@ * Author URI: https://infinitnet.io/ * Plugin URI: https://github.com/infinitnet/wordpress-date-shortcodes * Update URI: https://github.com/infinitnet/wordpress-date-shortcodes - * Version: 1.0 + * Version: 1.1 * License: GPLv3 * Text Domain: wordpress-date-shortcodes */ -// Shortcode functions for current year and current month -if ( ! function_exists( 'infinitnet_current_year_shortcode' ) ) { - function infinitnet_current_year_shortcode() { - return date_i18n( 'Y' ); - } - add_shortcode( 'currentyear', 'infinitnet_current_year_shortcode' ); -} +// Main function +if ( ! function_exists( 'infinitnet_date_shortcode' ) ) { + function infinitnet_date_shortcode( $type, $format, $adjustment = 0, $post_date = '' ) { + switch ( $type ) { + case 'current': + $date = date_i18n($format, strtotime("$adjustment days")); + break; + case 'published': + $date = get_the_date($format, get_the_ID()); + break; + case 'modified': + $date = get_the_modified_date($format, get_the_ID()); + break; + default: + $date = ''; + } -if ( ! function_exists( 'infinitnet_current_month_shortcode' ) ) { - function infinitnet_current_month_shortcode() { - return date_i18n( 'F' ); - } - add_shortcode( 'currentmonth', 'infinitnet_current_month_shortcode' ); -} + if ($type !== 'current' && $adjustment !== 0) { + $date = date_i18n($format, strtotime("$post_date $adjustment days")); + } -// Shortcode to display the published year -if ( ! function_exists( 'infinitnet_published_year_shortcode' ) ) { - function infinitnet_published_year_shortcode() { - global $post; - return get_the_date('Y', $post->ID); + return $date; } - add_shortcode( 'publishedyear', 'infinitnet_published_year_shortcode' ); } -// Shortcode to display the published month -if ( ! function_exists( 'infinitnet_published_month_shortcode' ) ) { - function infinitnet_published_month_shortcode() { - global $post; - return get_the_date('F', $post->ID); - } - add_shortcode( 'publishedmonth', 'infinitnet_published_month_shortcode' ); -} +// Year shortcodes +add_shortcode( 'currentyear', function() { return infinitnet_date_shortcode('current', 'Y'); }); +add_shortcode( 'publishedyear', function() { return infinitnet_date_shortcode('published', 'Y'); }); +add_shortcode( 'modifiedyear', function() { return infinitnet_date_shortcode('modified', 'Y'); }); -if ( ! function_exists( 'infinitnet_modified_year_shortcode' ) ) { - function infinitnet_modified_year_shortcode() { - global $post; - return get_the_modified_date('Y', $post->ID); - } - add_shortcode( 'modifiedyear', 'infinitnet_modified_year_shortcode' ); -} +// Month shortcodes +add_shortcode( 'currentmonth', function() { return infinitnet_date_shortcode('current', 'F'); }); +add_shortcode( 'publishedmonth', function() { return infinitnet_date_shortcode('published', 'F'); }); +add_shortcode( 'modifiedmonth', function() { return infinitnet_date_shortcode('modified', 'F'); }); -// Shortcode to display the last modified month -if ( ! function_exists( 'infinitnet_modified_month_shortcode' ) ) { - function infinitnet_modified_month_shortcode() { - global $post; - return get_the_modified_date('F', $post->ID); - } - add_shortcode( 'modifiedmonth', 'infinitnet_modified_month_shortcode' ); -} +// Day shortcodes +add_shortcode( 'currentday', function($atts) { + $atts = shortcode_atts( array('adjust' => 0), $atts, 'currentday' ); + $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; + return infinitnet_date_shortcode('current', $day_format, intval($atts['adjust'])); +}); +add_shortcode( 'publishedday', function($atts) { + $atts = shortcode_atts( array('adjust' => 0), $atts, 'publishedday' ); + $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; + return infinitnet_date_shortcode('published', $day_format, intval($atts['adjust']), get_the_date('Y-m-d')); +}); +add_shortcode( 'modifiedday', function($atts) { + $atts = shortcode_atts( array('adjust' => 0), $atts, 'modifiedday' ); + $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; + return infinitnet_date_shortcode('modified', $day_format, intval($atts['adjust']), get_the_modified_date('Y-m-d')); +}); // Unified function to process meta content with shortcodes function infinitnet_process_meta_content( $content ) {