Skip to content

Commit 09c9ebb

Browse files
author
infinitnet
committed
v1.1 release: new features and refactoring
1 parent 0bcf424 commit 09c9ebb

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ WordPress plugin that adds shortcodes for current, published, and last modified
44
## Purpose
55
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.
66

7+
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"]`!"
8+
79
## Available Shortcodes
10+
- **[currentday]** - current day
11+
- **[currentday adjust="+1"]** - tomorrow (both `+` and `-` are supported)
812
- **[currentmonth]** - current month
913
- **[currentyear]** - current year
14+
- **[publishedday]** - day the post/page was published
1015
- **[publishedmonth]** - month the post/page was published
1116
- **[publishedyear]** - year the post/page was published
17+
- **[modifiedday]** - day the post/page was last modified
1218
- **[modifiedmonth]** - month the post/page was last modified
1319
- **[modifiedyear]** - year the post/page was last modified
1420

wordpress-date-shortcodes.php

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,62 @@
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.0
9+
* Version: 1.1
1010
* License: GPLv3
1111
* Text Domain: wordpress-date-shortcodes
1212
*/
1313

14-
// Shortcode functions for current year and current month
15-
if ( ! function_exists( 'infinitnet_current_year_shortcode' ) ) {
16-
function infinitnet_current_year_shortcode() {
17-
return date_i18n( 'Y' );
18-
}
19-
add_shortcode( 'currentyear', 'infinitnet_current_year_shortcode' );
20-
}
14+
// Main function
15+
if ( ! function_exists( 'infinitnet_date_shortcode' ) ) {
16+
function infinitnet_date_shortcode( $type, $format, $adjustment = 0, $post_date = '' ) {
17+
switch ( $type ) {
18+
case 'current':
19+
$date = date_i18n($format, strtotime("$adjustment days"));
20+
break;
21+
case 'published':
22+
$date = get_the_date($format, get_the_ID());
23+
break;
24+
case 'modified':
25+
$date = get_the_modified_date($format, get_the_ID());
26+
break;
27+
default:
28+
$date = '';
29+
}
2130

22-
if ( ! function_exists( 'infinitnet_current_month_shortcode' ) ) {
23-
function infinitnet_current_month_shortcode() {
24-
return date_i18n( 'F' );
25-
}
26-
add_shortcode( 'currentmonth', 'infinitnet_current_month_shortcode' );
27-
}
31+
if ($type !== 'current' && $adjustment !== 0) {
32+
$date = date_i18n($format, strtotime("$post_date $adjustment days"));
33+
}
2834

29-
// Shortcode to display the published year
30-
if ( ! function_exists( 'infinitnet_published_year_shortcode' ) ) {
31-
function infinitnet_published_year_shortcode() {
32-
global $post;
33-
return get_the_date('Y', $post->ID);
35+
return $date;
3436
}
35-
add_shortcode( 'publishedyear', 'infinitnet_published_year_shortcode' );
3637
}
3738

38-
// Shortcode to display the published month
39-
if ( ! function_exists( 'infinitnet_published_month_shortcode' ) ) {
40-
function infinitnet_published_month_shortcode() {
41-
global $post;
42-
return get_the_date('F', $post->ID);
43-
}
44-
add_shortcode( 'publishedmonth', 'infinitnet_published_month_shortcode' );
45-
}
39+
// 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'); });
4643

47-
if ( ! function_exists( 'infinitnet_modified_year_shortcode' ) ) {
48-
function infinitnet_modified_year_shortcode() {
49-
global $post;
50-
return get_the_modified_date('Y', $post->ID);
51-
}
52-
add_shortcode( 'modifiedyear', 'infinitnet_modified_year_shortcode' );
53-
}
44+
// 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'); });
5448

55-
// Shortcode to display the last modified month
56-
if ( ! function_exists( 'infinitnet_modified_month_shortcode' ) ) {
57-
function infinitnet_modified_month_shortcode() {
58-
global $post;
59-
return get_the_modified_date('F', $post->ID);
60-
}
61-
add_shortcode( 'modifiedmonth', 'infinitnet_modified_month_shortcode' );
62-
}
49+
// Day shortcodes
50+
add_shortcode( 'currentday', function($atts) {
51+
$atts = shortcode_atts( array('adjust' => 0), $atts, 'currentday' );
52+
$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']));
54+
});
55+
add_shortcode( 'publishedday', function($atts) {
56+
$atts = shortcode_atts( array('adjust' => 0), $atts, 'publishedday' );
57+
$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'));
59+
});
60+
add_shortcode( 'modifiedday', function($atts) {
61+
$atts = shortcode_atts( array('adjust' => 0), $atts, 'modifiedday' );
62+
$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'));
64+
});
6365

6466
// Unified function to process meta content with shortcodes
6567
function infinitnet_process_meta_content( $content ) {

0 commit comments

Comments
 (0)