How to Fix Shortcodes Ultimate Button Download and Click Tracking Issues
Content
Many users of the 'WP Shortcodes Plugin — Shortcodes Ultimate' rely on its powerful button shortcode for file downloads and user interactions. However, a common point of confusion arises when trying to make these buttons trigger a direct download or execute custom JavaScript code, like sending an email or tracking a click. This guide will explain why these issues occur and provide the most effective solutions.
Common Problems and Their Causes
Based on community reports, the primary issues are:
- Files Opening Instead of Downloading: A button with a
downloadattribute may open a PDF or audio file in the browser instead of triggering a download. This behavior is often dictated by the user's specific browser settings and how the web server is configured to serve that specific file type, not necessarily a fault of the shortcode. - Custom JavaScript Not Firing: Users attempting to add advanced
onClickactions (like sending an email, showing a thank you message, or integrating with Google Analytics) find their code doesn't work. This is typically because the JavaScript code is placed incorrectly or conflicts with other site scripts. - Redirects After Download Not Working: It is not natively possible for a single button click to both force a file download and redirect the user's browser to a new page simultaneously due to fundamental browser security limitations.
Solutions and Workarounds
1. Ensuring Files Download Instead of Open
While the shortcode's download attribute provides a hint to the browser, the final action is not guaranteed. To improve reliability:
- Check Server Configuration: Ensure your server sends the correct headers for the file type. For PDFs, this should be
Content-Disposition: attachmentandContent-Type: application/pdf. You may need to add rules to your.htaccessfile (on Apache servers) or contact your hosting provider. - Use a Download Manager Plugin: Consider using a dedicated WordPress download manager plugin that handles file delivery and forces downloads more reliably than a browser might on its own.
2. Adding Custom onClick JavaScript Actions
The 'Advanced JavaScript code for onClick action' field in the button shortcode is meant for simple, inline JavaScript. For more complex scripts, a different approach is required to avoid errors.
Correct Method for Adding Custom Tracking or Alerts:
- Add a Custom Class: First, give your button a unique class using the
classparameter in the shortcode.
[su_button url="/path/to/file.pdf" class="track-download"]Download File[/su_button] - Enqueue Your Script Properly: Do not paste large JavaScript functions into the shortcode's onClick field. Instead, add the following code to your theme's
functions.phpfile or a custom functionality plugin. This code safely adds the script to your site and uses jQuery to listen for clicks on buttons with your custom class.
function su_custom_button_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('body').on('click', '.track-download', function() {
// Your custom code here. Examples:
// Example 1: Send GA event
// ga('send', 'event', 'Buttons', 'clicked', 'Download Button');
// Example 2: Show an alert thank you message
alert('Thank you for your download!');
// Example 3: Replace button text after click (after a brief delay)
var button = $(this);
setTimeout(function() {
button.text('Thank You!');
}, 100);
});
});
</script>
<?php
}
add_action('wp_footer', 'su_custom_button_script');
Important Note on Sending Emails: Sending an email directly via client-side JavaScript is not possible. This requires server-side processing (PHP). To track a button click and send an email, you would need to use WordPress's wp_ajax_ hooks to create a custom AJAX handler. This is an advanced development task that typically requires custom coding beyond the scope of the shortcode plugin itself.
3. The Download + Redirect Limitation
As noted in the threads, a single click cannot both download a file and redirect the user. A common workaround is to have the button redirect the user to a dedicated "Thank You" page, which then automatically triggers the download using JavaScript or meta refresh tags. This provides a smoother user experience than trying to force both actions from one link.
Conclusion
While the Shortcodes Ultimate button shortcode is versatile, achieving advanced functionality like forced downloads, click tracking, and dynamic messages requires understanding the interaction between browser behavior, JavaScript, and WordPress development practices. Forcing a download is often a server-side issue, while custom click actions require correctly enqueued scripts. For highly specific needs like server-side email notifications, custom PHP development is usually necessary.
Related Support Threads Support
-
Insert PDF document casehttps://wordpress.org/support/topic/insert-pdf-document-case/
-
send admin mail when button is clickcedhttps://wordpress.org/support/topic/send-admin-mail-when-button-is-clickced/
-
QR code showing wrong outputhttps://wordpress.org/support/topic/qr-code-showing-wrong-output/
-
Dowload button for embedded document.https://wordpress.org/support/topic/dowload-button-for-embedded-document/
-
How can I use button short code downloads file then redirect another page??https://wordpress.org/support/topic/how-can-i-use-button-short-code-downloads-file-then-redirect-another-page/
-
onClickをボタンで使いたいhttps://wordpress.org/support/topic/onclick%e3%82%92%e3%83%9c%e3%82%bf%e3%83%b3%e3%81%a7%e4%bd%bf%e3%81%84%e3%81%9f%e3%81%84/
-
Button to download files not workinghttps://wordpress.org/support/topic/button-to-download-files-not-working/
-
“Thank You” message after Button onClickhttps://wordpress.org/support/topic/thank-you-message-after-button-onclick/
-
Rating functionhttps://wordpress.org/support/topic/rating-function-2/
-
Send email with a su_button is clickedhttps://wordpress.org/support/topic/send-email-with-a-su_button-is-clicked/