Custom PayPal Donate Button, WordPress

Add this code to your functions.php or snippet plugin.

This code finds buttons that has “.donate” class and when those buttons clicked opens the Paypal donate page.

Change the email and name fields according to your needs.

function custom_donate_button_script() {
    ?>
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        // Find all the buttons with the class '.donate' in the document
        var donateButtons = document.querySelectorAll('.donate');

        // Iterate over each donate button
        donateButtons.forEach(function(button) {
            // Add click event listener to each donate button
            button.addEventListener('click', function() {
                // Create a new form element
                var form = document.createElement('form');
                form.action = 'https://www.paypal.com/donate';
                form.method = 'post';
                form.target = '_top';

                // Add the necessary input fields to the form
                form.innerHTML = `
                    <input type="hidden" name="business" value="donations@stelp.eu">
                    <input type="hidden" name="no_recurring" value="0">
                    <input type="hidden" name="item_name" value="STELP e. V.">
                    <input type="hidden" name="item_number" value="STELP Donation">
                    <input type="hidden" name="currency_code" value="EUR">
                    <img alt="" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif">
                `;

                // Append the form to the document body
                document.body.appendChild(form);

                // Submit the form
                form.submit();

                // Optionally, remove the form after submission
                form.remove();
            });
        });
    });
    </script>
    <?php
}
add_action('wp_footer', 'custom_donate_button_script');

Leave the first comment