Last active
September 10, 2024 00:35
-
-
Save rafaehlers/76bed8ab50a98d8dbca17f91748c83ad to your computer and use it in GitHub Desktop.
Shortcode to output a download button after receiving a file upload field Merge Tag. It supports multiple files.
This file contains hidden or 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 // DO NOT COPY THIS LINE | |
// How to use: inside a custom content field, use: [download_links urls="{File Upload:9}"] | |
function shortcode_download_links($atts) { | |
// Capture the shortcode attributes | |
$atts = shortcode_atts( | |
array( | |
'urls' => '', // Default value for the merge tag | |
), | |
$atts | |
); | |
// Receive the value from the merge tag {upload file:9} | |
$urls = $atts['urls']; | |
// Check if the value is empty | |
if (empty($urls)) { | |
return ''; // Return empty if there are no URLs | |
} | |
// Split the URLs using the <br> tag as a delimiter | |
$urls_array = preg_split('/<br\s*\/?>/i', $urls); | |
// Initialize a string to store the buttons | |
$output = ''; | |
// For each URL, create a download button | |
foreach ($urls_array as $url) { | |
// Remove whitespace from the URL | |
$url = trim($url); | |
// Check if the URL is not empty | |
if (!empty($url)) { | |
$output .= '<a class="download" href="' . esc_url($url) . '" download="">Download</a><br>'; | |
} | |
} | |
// Return the HTML with the buttons | |
return $output; | |
} | |
// Register the shortcode [download_links urls="{upload file:9}"] | |
add_shortcode('download_links', 'shortcode_download_links'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment