Skip to content

Commit

Permalink
v1.1 release: new features and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
infinitnet committed Nov 17, 2023
1 parent 0bcf424 commit 09c9ebb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
88 changes: 45 additions & 43 deletions wordpress-date-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit 09c9ebb

Please sign in to comment.