Created
July 16, 2024 12:10
-
-
Save hmowais/2fe0f46156bde3d37578a0a856d24377 to your computer and use it in GitHub Desktop.
Add to Calender for WordPress Shortcode
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 | |
/* Event Add to Calendar Shortcode */ | |
// Shortcode function to display Events | |
function display_events() { | |
ob_start(); | |
// Get the current post ID | |
$current_post_id = get_the_ID(); | |
// Arguments for WP_Query | |
$args = array( | |
'post_type' => 'events', | |
'p' => $current_post_id, // Query only the current post | |
); | |
// Custom query | |
$query = new WP_Query($args); | |
// Check if any posts were found | |
if ($query->have_posts()) { ?> | |
<div class="addto-calendar"> | |
<?php | |
// Loop through the posts | |
while ($query->have_posts()) { | |
$query->the_post(); | |
// Retrieve title, description, and custom field | |
$title = get_the_title(); | |
$description = get_the_content(); | |
$event_start_date = get_field('event_start_date'); | |
$event_end_date = get_field('event_end_date'); | |
$event_start_time = get_field('event_start_time'); | |
$event_end_time = get_field('event_end_time'); | |
$startDate_timestamp = strtotime($event_start_date); | |
$endDate_timestamp = strtotime($event_end_date); | |
$startTime_timestamp = strtotime($event_start_time); | |
$endTime_timestamp = strtotime($event_end_time); | |
// Format the date as Y/m/d | |
$formatted_event_start_date = date_i18n('Ymd', $startDate_timestamp); | |
$formatted_event_end_date = date_i18n('Ymd', $endDate_timestamp); | |
$formatted_event_start_time = date('His', $startTime_timestamp); | |
$formatted_event_end_time = date('His', $endTime_timestamp); | |
$google_calendar_link = "https://www.google.com/calendar/render?action=TEMPLATE&text=" . urlencode($title) . "&details=" . urlencode($description) . "&location=" . urlencode($title) . "&dates=" . $formatted_event_start_date . "T" . $formatted_event_start_time . "/" . $formatted_event_end_date . "T" . $formatted_event_end_time; | |
// Outlook Calendar link | |
$outlook_calendar_link = "https://outlook.live.com/calendar/0/deeplink/compose?subject=" . urlencode($title) . "&body=" . urlencode($description) . "&location=" . urlencode($title) . "&startdt=" . $formatted_event_start_date . "T" . $formatted_event_start_time . "Z&enddt=" . $formatted_event_end_date . "T" . $formatted_event_end_time . "Z&path=/calendar/action/compose"; | |
// Yahoo Calendar link | |
$yahoo_calendar_link = "https://calendar.yahoo.com/?v=60&view=d&type=20&title=" . urlencode($title) . "&st=" . $formatted_event_start_date . "T" . $formatted_event_start_time . "Z&dur=0100&desc=" . urlencode($description) . "&in_loc=" . urlencode($title); | |
// Apple Calendar (iCal) link | |
$ical_content = "BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\nUID:" . uniqid() . "\nDTSTAMP:" . gmdate('Ymd\THis\Z') . "\nDTSTART:" . $formatted_event_start_date . "T" . $formatted_event_start_time . "Z\nDTEND:" . $formatted_event_end_date . "T" . $formatted_event_end_time . "Z\nSUMMARY:" . $title . "\nDESCRIPTION:" . $description . "\nLOCATION:" . $title . "\nEND:VEVENT\nEND:VCALENDAR"; | |
$ical_file = "data:text/calendar;charset=utf8," . urlencode($ical_content); | |
?> | |
<a href="<?php echo $google_calendar_link; ?>" title="Google Calendar" target="_blank"><i class="fa-brands fa-google"></i></a> | |
<a href="<?php echo $yahoo_calendar_link; ?>" title="Yahoo Calendar" target="_blank"><img src="https://fairelectionscenter.org/wp-content/uploads/2024/07/yahoo.svg"></a> | |
<a href="<?php echo $outlook_calendar_link; ?>" title="Outlook Calendar" target="_blank"><img src="https://fairelectionscenter.org/wp-content/uploads/2024/07/outlook.svg"></a> | |
<a href="<?php echo $ical_file; ?>" title="Apple Calendar" target="_blank"><i class="fa-brands fa-apple"></i></a> | |
<?php | |
} | |
// Restore original post data | |
?> | |
</div> | |
<?php } | |
wp_reset_postdata(); | |
$myvariable = ob_get_clean(); | |
return $myvariable; | |
} | |
add_shortcode('display_events', 'display_events'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment