|
6 | 6 | * Author URI: https://infinitnet.io/ |
7 | 7 | * Plugin URI: https://github.com/infinitnet/wordpress-date-shortcodes |
8 | 8 | * Update URI: https://github.com/infinitnet/wordpress-date-shortcodes |
9 | | - * Version: 1.0 |
| 9 | + * Version: 1.1 |
10 | 10 | * License: GPLv3 |
11 | 11 | * Text Domain: wordpress-date-shortcodes |
12 | 12 | */ |
13 | 13 |
|
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 | + } |
21 | 30 |
|
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 | + } |
28 | 34 |
|
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; |
34 | 36 | } |
35 | | - add_shortcode( 'publishedyear', 'infinitnet_published_year_shortcode' ); |
36 | 37 | } |
37 | 38 |
|
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'); }); |
46 | 43 |
|
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'); }); |
54 | 48 |
|
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 | +}); |
63 | 65 |
|
64 | 66 | // Unified function to process meta content with shortcodes |
65 | 67 | function infinitnet_process_meta_content( $content ) { |
|
0 commit comments