Skip to content

Commit 2d8727f

Browse files
author
infinitnet
committed
v1.2 release: adjust parameter rolled out to all shortcodes
1 parent 09c9ebb commit 2d8727f

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ The main purpose of this plugin is to dynamically add dates to post titles. The
66

77
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"]`!"
88

9+
All other shortcodes also support the `adjust` parameter, e.g. `[currentmonth adjust="+1"]` will show the month after the current one, `[currentyear adjust="+1"]` will show the next year, etc.
10+
911
## Available Shortcodes
10-
- **[currentday]** - current day
1112
- **[currentday adjust="+1"]** - tomorrow (both `+` and `-` are supported)
13+
- **[currentday]** - current day
1214
- **[currentmonth]** - current month
1315
- **[currentyear]** - current year
1416
- **[publishedday]** - day the post/page was published
@@ -18,6 +20,8 @@ You can use the `adjust` parameter for `[currentday]` to display the date for to
1820
- **[modifiedmonth]** - month the post/page was last modified
1921
- **[modifiedyear]** - year the post/page was last modified
2022

23+
**Note:** The `adjust` parameter is supported by all shortcodes and can use both `+` and `-` depending on whether you want to display future or past dates.
24+
2125
## Compatibility
2226
The WordPress Date Shortcodes plugin is compatible with:
2327
- Rank Math

wordpress-date-shortcodes.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,69 @@
66
* Author URI: https://infinitnet.io/
77
* Plugin URI: https://github.com/infinitnet/wordpress-date-shortcodes
88
* Update URI: https://github.com/infinitnet/wordpress-date-shortcodes
9-
* Version: 1.1
9+
* Version: 1.2
1010
* License: GPLv3
1111
* Text Domain: wordpress-date-shortcodes
1212
*/
1313

1414
// Main function
1515
if ( ! function_exists( 'infinitnet_date_shortcode' ) ) {
16-
function infinitnet_date_shortcode( $type, $format, $adjustment = 0, $post_date = '' ) {
16+
function infinitnet_date_shortcode( $type, $format, $atts ) {
17+
$atts = shortcode_atts( array('adjust' => 0), $atts );
18+
$adjustment = intval($atts['adjust']);
19+
1720
switch ( $type ) {
1821
case 'current':
19-
$date = date_i18n($format, strtotime("$adjustment days"));
22+
if ($format == 'Y') {
23+
$adjusted_date = strtotime("$adjustment year");
24+
} elseif ($format == 'F') {
25+
$adjusted_date = strtotime("$adjustment month");
26+
} else {
27+
$adjusted_date = strtotime("$adjustment day");
28+
}
29+
$date = date_i18n($format, $adjusted_date);
2030
break;
2131
case 'published':
22-
$date = get_the_date($format, get_the_ID());
23-
break;
2432
case 'modified':
25-
$date = get_the_modified_date($format, get_the_ID());
33+
$post_date = ($type == 'published') ? get_the_date('Y-m-d', get_the_ID()) : get_the_modified_date('Y-m-d', get_the_ID());
34+
if ($format == 'Y') {
35+
$date = date_i18n($format, strtotime("$post_date +$adjustment year"));
36+
} elseif ($format == 'F') {
37+
$date = date_i18n($format, strtotime("$post_date +$adjustment month"));
38+
} else {
39+
$date = date_i18n($format, strtotime("$post_date +$adjustment day"));
40+
}
2641
break;
2742
default:
2843
$date = '';
2944
}
3045

31-
if ($type !== 'current' && $adjustment !== 0) {
32-
$date = date_i18n($format, strtotime("$post_date $adjustment days"));
33-
}
34-
3546
return $date;
3647
}
3748
}
3849

3950
// Year shortcodes
40-
add_shortcode( 'currentyear', function() { return infinitnet_date_shortcode('current', 'Y'); });
41-
add_shortcode( 'publishedyear', function() { return infinitnet_date_shortcode('published', 'Y'); });
42-
add_shortcode( 'modifiedyear', function() { return infinitnet_date_shortcode('modified', 'Y'); });
51+
add_shortcode( 'currentyear', function($atts) { return infinitnet_date_shortcode('current', 'Y', $atts); });
52+
add_shortcode( 'publishedyear', function($atts) { return infinitnet_date_shortcode('published', 'Y', $atts); });
53+
add_shortcode( 'modifiedyear', function($atts) { return infinitnet_date_shortcode('modified', 'Y', $atts); });
4354

4455
// Month shortcodes
45-
add_shortcode( 'currentmonth', function() { return infinitnet_date_shortcode('current', 'F'); });
46-
add_shortcode( 'publishedmonth', function() { return infinitnet_date_shortcode('published', 'F'); });
47-
add_shortcode( 'modifiedmonth', function() { return infinitnet_date_shortcode('modified', 'F'); });
56+
add_shortcode( 'currentmonth', function($atts) { return infinitnet_date_shortcode('current', 'F', $atts); });
57+
add_shortcode( 'publishedmonth', function($atts) { return infinitnet_date_shortcode('published', 'F', $atts); });
58+
add_shortcode( 'modifiedmonth', function($atts) { return infinitnet_date_shortcode('modified', 'F', $atts); });
4859

4960
// Day shortcodes
5061
add_shortcode( 'currentday', function($atts) {
51-
$atts = shortcode_atts( array('adjust' => 0), $atts, 'currentday' );
5262
$day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j';
53-
return infinitnet_date_shortcode('current', $day_format, intval($atts['adjust']));
63+
return infinitnet_date_shortcode('current', $day_format, $atts);
5464
});
5565
add_shortcode( 'publishedday', function($atts) {
56-
$atts = shortcode_atts( array('adjust' => 0), $atts, 'publishedday' );
5766
$day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j';
58-
return infinitnet_date_shortcode('published', $day_format, intval($atts['adjust']), get_the_date('Y-m-d'));
67+
return infinitnet_date_shortcode('published', $day_format, $atts);
5968
});
6069
add_shortcode( 'modifiedday', function($atts) {
61-
$atts = shortcode_atts( array('adjust' => 0), $atts, 'modifiedday' );
6270
$day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j';
63-
return infinitnet_date_shortcode('modified', $day_format, intval($atts['adjust']), get_the_modified_date('Y-m-d'));
71+
return infinitnet_date_shortcode('modified', $day_format, $atts);
6472
});
6573

6674
// Unified function to process meta content with shortcodes

0 commit comments

Comments
 (0)