This guide will walk you through the process of using the Advanced Custom Fields (ACF) plugin in WordPress to dynamically populate the event details for your AddCal Smart Links. This is incredibly useful for creating event listings, appointment booking pages, or any content where event details are managed within WordPress and need to be easily added to a user's calendar via AddCal.
Instead of manually creating a Smart Link for every event or hardcoding details, this integration allows you to:
Before you begin, ensure you have the following:
First, you need to set up your Smart Link in the AddCal dashboard.
Now, you'll create the ACF fields that will hold your event details in WordPress.
Now, create or edit a WordPress post/page and populate your newly created ACF fields with event data.
This step will guide you through creating a WordPress shortcode. This allows you to easily insert the AddCal button into any post, page, or even widget areas using the block editor or classic editor, without touching theme files directly for each instance.
<?php
/**
* Custom Shortcode for AddCal Button with ACF Integration
*
* This shortcode pulls event details from Advanced Custom Fields
* and generates the AddCal embed button.
*
* Usage: [addcal_button smart_link_key="YOUR_SMART_LINK_KEY"]
* Replace YOUR_SMART_LINK_KEY with your actual AddCal Smart Link Key.
*/
function addcal_acf_button_shortcode($atts) {
// Define default attributes for the shortcode
$atts = shortcode_atts(
array(
'smart_link_key' => '', // MANDATORY: User must provide their Smart Link Key
),
$atts,
'addcal_button'
);
$smart_link_key = esc_attr($atts['smart_link_key']);
// Check if a Smart Link Key is provided
if (empty($smart_link_key)) {
return '<p style="color: red;">Error: AddCal Smart Link Key is missing for the `[addcal_button]` shortcode.</p>';
}
// Output buffer to capture HTML
ob_start();
// Ensure ACF functions are available before proceeding.
if (function_exists('get_field')) :
// Get the current post ID. For shortcodes, it's usually the global post ID.
$post_id = get_the_ID();
// Retrieve ACF field values for the current post.
// Ensure these field names match what you set up in Step 2 of the guide.
$event_title = get_field('event_title', $post_id);
$event_start_date = get_field('event_start_date', $post_id); // Format: YYYY-MM-DDTHH:mm:ss
$event_end_date = get_field('event_end_date', $post_id); // Format: YYYY-MM-DDTHH:mm:ss
$event_duration = get_field('event_duration', $post_id); // In minutes
$is_all_day = get_field('is_all_day_event', $post_id); // Boolean (true/false) or 0/1
$event_timezone = get_field('event_timezone', $post_id);
$event_location = get_field('event_location', $post_id);
$event_description = get_field('event_description', $post_id);
// --- AddCal Specific Formatting and Logic ---
// Convert boolean 'is_all_day_event' to 'true'/'false' string for AddCal
$is_all_day_string = $is_all_day ? 'true' : 'false';
// Handle date_end vs. duration logic based on AddCal parameters
$end_time_element = '';
if (!empty($event_duration)) {
// If duration is set, prioritize it over end date
$end_time_element = '<span class="duration">' . esc_attr($event_duration) . '</span>';
} elseif (!empty($event_end_date)) {
// Otherwise, use the end date if available
$end_time_element = '<span class="end">' . esc_attr($event_end_date) . '</span>';
}
// Check if the essential fields for an event exist before displaying the button
if (!empty($event_title) && !empty($event_start_date)) :
?>
<div class="addcal-button-container" style="text-align: center; margin-top: 20px;">
<div class="addcal-btn"
data-base="addcal.co"
data-smart-link-key="<?php echo $smart_link_key; ?>"
data-theme="default">
<?php if (!empty($event_title)) : ?>
<span class="title"><?php echo esc_html($event_title); ?></span>
<?php endif; ?>
<?php if (!empty($event_start_date)) : ?>
<span class="start"><?php echo esc_attr($event_start_date); ?></span>
<?php endif; ?>
<?php echo $end_time_element; // This will be duration OR end date ?>
<?php if (!empty($event_timezone)) : ?>
<span class="timezone"><?php echo esc_attr($event_timezone); ?></span>
<?php endif; ?>
<?php if (!empty($event_description)) : ?>
<span class="description"><?php echo esc_html($event_description); ?></span>
<?php endif; ?>
<?php if (!empty($event_location)) : ?>
<span class="location"><?php echo esc_html($event_location); ?></span>
<?php endif; ?>
<span class="is_all_day"><?php echo esc_attr($is_all_day_string); ?></span>
</div>
<script src="https://cdn.addcal.co/embed/1.1.0/button.min.js" async defer></script>
</div>
<?php
else :
// Message if essential event data is missing
echo '<p style="color: gray;">AddCal button not displayed: Essential event details are missing for this content. Please complete the event ACF fields.</p>';
endif; // End check for essential fields
else :
// Message if ACF is not active
echo '<p style="color: red;">Error: Advanced Custom Fields (ACF) plugin is not active. The AddCal shortcode requires ACF.</p>';
endif; // End function_exists('get_field') check
return ob_get_clean(); // Return the captured HTML
}
add_shortcode('addcal_button', 'addcal_acf_button_shortcode');
?>
Now you can use the shortcode in any post, page, or text widget:
[addcal_button smart_link_key="YOUR_SMART_LINK_KEY"]
When the page is viewed on the front end, WordPress will process this shortcode, retrieve the ACF fields associated with that post/page, and generate the AddCal button with the dynamic event data.
By following these steps, you'll have a robust system for generating AddCal Smart Links directly from your WordPress content using Advanced Custom Fields, significantly enhancing your event management capabilities.
Last updated on June 21, 2025