Created
March 5, 2024 11:39
-
-
Save andrasguseo/f78d45739d1ced98af48f76b38a654d0 to your computer and use it in GitHub Desktop.
ECP > Set recurring event end date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change the end date for recurring events from 1 year in the future to something else. | |
* IMPORTANT: Works with Classic Editor only! | |
* | |
* Usage: Add the snippet to your functions.php file or with a plugin like Code Snippets. | |
* | |
* @author Andras Guseo | |
* | |
* Plugins required: Events Calendar Pro | |
* Created: March 1, 2024 | |
*/ | |
function tec_custom_end_date() { | |
$current_screen = get_current_screen(); | |
// Load only when adding a new event. | |
if ( $current_screen->parent_base === 'edit' | |
&& isset ( $_GET[ 'post_type' ] ) | |
&& $_GET[ 'post_type' ] === 'tribe_events' | |
) { | |
// Get the saved datepicker format. | |
$datepicker_format = Tribe__Date_Utils::datepicker_formats( tribe_get_option( 'datepickerFormat' ) ); | |
// Convert it to JS date format. | |
$js_datepicker_format = strtolower( php_to_js_date_format( $datepicker_format ) ); | |
?> | |
<script> | |
function waitForElement(selector, callback) { | |
console.log('Waiting for element...'); | |
let element; | |
let intervalID = setInterval(function () { | |
//console.log('Highlight: Selector: ' + selector); | |
element = document.querySelectorAll(selector); | |
//console.log('Highlight: element: ' + element[0]); | |
if (element[0]) { | |
console.log('Element found'); | |
clearInterval(intervalID); | |
callback(); | |
} | |
}, 500); // Check every 500 milliseconds | |
} | |
function modifyEndDate() { | |
// Get today's date | |
let futureDate = new Date(); | |
// If you want to add days... | |
// Number of days to add | |
const daysToAdd = 14; | |
// Add days to today's date | |
futureDate.setDate(futureDate.getDate() + daysToAdd); | |
// If you want to add months... | |
// Number of months to add | |
// const monthsToAdd = 3; | |
// Add months to today's date | |
// futureDate.setMonth(futureDate.getMonth() + monthsToAdd); | |
console.log("Future date: " + futureDate); | |
// Format the date as YYYY-MM-DD | |
formattedDate = futureDate.format('<?php echo $js_datepicker_format ?>'); | |
console.log("Formatted date: " + formattedDate); | |
document.querySelector('.recurrence-row .tribe-field-end_date').value = formattedDate; | |
} | |
waitForElement('.recurrence-row .tribe-field-end_date', modifyEndDate); | |
</script> | |
<?php | |
} | |
} | |
/** | |
* Convert a PHP date format to a JS date format. | |
* | |
* @param string $php_format The PHP date format. | |
* | |
* @return string The JS date format. | |
*/ | |
function php_to_js_date_format( $php_format ) { | |
$format_map = [ | |
'd' => 'dd', // The day of the month (from 01 to 31) | |
'j' => 'd', // The day of the month without leading zeros (1 to 31) | |
'm' => 'mm', // A numeric representation of a month (from 01 to 12) | |
'n' => 'm', // A numeric representation of a month, without leading zeros (1 to 12) | |
'Y' => 'yyyy', // A four digit representation of a year | |
'y' => 'yy', // A two digit representation of a year | |
]; | |
$js_format = strtr( $php_format, $format_map ); | |
return $js_format; | |
} | |
add_action( 'admin_footer', 'tec_custom_end_date' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment