search
HomeCMS TutorialWordPressUsing WordPress to collect donations: Bitcoin

Using WordPress to collect donations: Bitcoin

In the second and final part of this mini-series, "Collecting Donations with WordPress," you'll learn how to write a WordPress plugin that allows users to send you donations via Bitcoin.

  • Part 1 - "Collecting Donations Using WordPress: PayPal"

The plugin uses its own backend settings panel and is highly customizable.

So, let’s get started!

Initialization plug-in

step 1

In your website’s wp-content/plugins directory, create a new folder called donate-bitcoins.

Step 2

Now, create a file called donate-bitcoins.php in that folder.

Step 3

Finally, you need to add the plugin header information, which will tell WordPress that your new plugin actually exists on your server. You can change these details to anything you want, but they should generally be in that order and contain minimal information.

<?php
/*
Plugin Name: Bitcoin Donate
Plugin URI: https://code.tutsplus.com
Description: Simple Bitcoin donation plugin.
Version: 1.0.0
Author: Sam Berson
Author URI: http://www.samberson.com/
*/

Step 4

You will now see the new plugin displayed in the Plugins page of your WordPress admin. Go ahead and activate the plugin, although you won't see much happening yet.

Add shortcode

You can use the donate button in any post or page you create using a simple shortcode. Essentially, a shortcode is a small piece of text, enclosed in square brackets, that allows you to call any function or action from a plugin or theme in the post editor.

In this plugin, the shortcode is [donate] and can be added anywhere in your post or page.

step 1

To add a shortcode to WordPress you need to use the add_shortcode function and define the shortcode there (in this case "Donate") and then you will define some option information. Since we will be outputting HTML, we need to start tracking the output. You also need to close PHP brackets before the next section.

function bitcoin_donate_shortcode() {

    $donate_options = get_option( 'bitcoin_donate_options' );

    $address = $donate_options['bitcoin_address'];
    $counter = $donate_options['bitcoin_counter'];

    ob_start();

    ?>

Step 2

Now, you will call the CoinWidget script in the plugin and define some JavaScript information. Then, reopen the PHP tag, capture the output, and close the function.

    <script src="http://coinwidget.com/widget/coin.js"></script>
    <script>
        CoinWidgetCom.go({
            wallet_address: '<?php echo $address; ?>',
            currency: 'bitcoin',
            counter: '<?php echo $counter; ?>',
            alignment: 'bl',
            qrcode: true,
            auto_show: false,
            lbl_button: '<?php _e( 'Donate', 'bitcoin_donate' ) ?>',
            lbl_address: '<?php _e( 'My Bitcoin Address:', 'bitcoin_donate' ) ?>',
            lbl_count: 'donations',
            lbl_amount: 'BTC'
        });
    </script>
    <?php

    return ob_get_clean();
}

Bitcoin wallet information

You will now set up some information for the Setup form, which will allow you to set up your Bitcoin wallet information.

step 1

You can first define a new function called bitcoin_donate_wallet_address() and use the get_option() function.

function bitcoin_donate_wallet_address() {

    $options = get_option( 'bitcoin_donate_options' );

    echo "<input name='bitcoin_donate_options[bitcoin_address]' type='text' value='{$options['bitcoin_address']}'/>";

}

Step 2

Let's go ahead and add a new function called bitcoin_donate_counter(), which defines the drop-down options in the settings panel, allowing you to set which digital donation buttons are displayed next to: "Transaction Count", "Receive to the amount" or "hide".

function bitcoin_donate_counter() {

    $options = get_option( 'bitcoin_donate_options' );

    ?>
    <p>
        <label>
            <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value="count" <?php checked( $options['bitcoin_counter'], 'count', true ); ?> /> <?php _e( 'Transaction Count', 'bitcoin_donate' ) ?>
        </label>
    </p>
    <p>
        <label>
            <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value= "amount" <?php checked( $options['bitcoin_counter'], 'amount', true ); ?> /> <?php _e( 'Amount Received', 'bitcoin_donate' ) ?>
        </label>
    </p>
    <p>
        <label>
            <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value= "hide" <?php checked( $options['bitcoin_counter'], 'hide', true ); ?> /> <?php _e( 'Hidden', 'bitcoin_donate' ) ?>
        </label>
    </p>
    <?php

}

Step 3

You should now add an empty callback, this is required to ensure the plugin functions properly. It just defines a new WordPress function, turns it on, and then turns it off again.

function bitcoin_donate_callback() {

    // Optional Callback.

}

Connect it all

Now that you have generated the shortcode and form fields, you need to connect them back to your WordPress admin for the plugin to function properly.

step 1

You should first register the plugin's settings and fields with the backend by adding the following code. In a nutshell, this code tells WordPress what to display in the admin.

function bitcoin_donate_register_settings_and_fields() {

    register_setting( 'bitcoin_donate_options', 'bitcoin_donate_options' );

    add_settings_section( 'bitcoin_donate_settings_section', __( 'Main Settings', 'bitcoin_donate' ), 'bitcoin_donate_callback', __FILE__ );

    add_settings_field( 'bitcoin_address', __( 'Bitcoin Address:', 'bitcoin_donate' ), 'bitcoin_donate_wallet_address', __FILE__, 'bitcoin_donate_settings_section' );

    add_settings_field( 'bitcoin_counter', __( 'What should the counter show?', 'bitcoin_donate' ), 'bitcoin_donate_counter', __FILE__, 'bitcoin_donate_settings_section' );

}

add_action( 'admin_init', 'bitcoin_donate_register_settings_and_fields' );

Step 2

Now you will tell WordPress what HTML to use when displaying the Settings form on the backend.

function bitcoin_donate_options_markup() {

    ?>
    <div class="wrap">
        <h2><?php _e( 'Bitcoin Donate Options', 'bitcoin_donate' ) ?></h2>
        <form method="post" action="options.php" enctype="multipart/form-data">
            <?php
                settings_fields( 'bitcoin_donate_options' );
                do_settings_sections( __FILE__ );
            ?>
            <p class="submit">
                <input type="submit" class="button-primary" name="submit" value="<?php _e( 'Save Changes', 'bitcoin_donate' ) ?>">
            </p>
        </form>
    </div>
    <?php
    
}

Step 3

Finally, you will tell WordPress what the Settings page is called, which user role can access it, and what HTML (as defined above) to use.

function bitcoin_donate_initialize_options() {

    add_options_page( __( 'Bitcoin Donate Options', 'bitcoin_donate' ), __( 'Bitcoin Donate Options', 'bitcoin_donate' ), 'administrator', __FILE__, 'bitcoin_donate_options_markup' );
}

add_action( 'admin_menu', 'bitcoin_donate_initialize_options' );

Final source code

By adding the [donate] shortcode to your post or page, your plugin should now work! Here is the complete source code of the plugin:


    

Summarize

You have now learned how to develop another brand new plugin that allows users to donate via Bitcoin. You can now initialize the plugin, use shortcodes, and add a settings page to your WordPress admin.

If you have any questions, please feel free to leave a message below and I will definitely help you!

The above is the detailed content of Using WordPress to collect donations: Bitcoin. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to add a comment box to WordPressHow to add a comment box to WordPressApr 20, 2025 pm 12:15 PM

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy sub-sites from wordpressHow to copy sub-sites from wordpressApr 20, 2025 pm 12:12 PM

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

How to write a header of a wordpressHow to write a header of a wordpressApr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to display wordpress commentsHow to display wordpress commentsApr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

How to upload source code for wordpressHow to upload source code for wordpressApr 20, 2025 pm 12:03 PM

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

How to copy wordpress codeHow to copy wordpress codeApr 20, 2025 pm 12:00 PM

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

What to do if there is an error in wordpressWhat to do if there is an error in wordpressApr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to close comments with wordpressHow to close comments with wordpressApr 20, 2025 am 11:54 AM

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version