Table of Contents
Initialization plug-in
step 1
Step 3
Add shortcode
Bitcoin wallet information
Connect it all
Final source code
Summarize
Home CMS Tutorial WordPress Using WordPress to collect donations: Bitcoin

Using WordPress to collect donations: Bitcoin

Sep 01, 2023 pm 05:29 PM
wordpress Bitcoin Collect donations

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/
*/
Copy after login

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();

    ?>
Copy after login

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();
}
Copy after login

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']}'/>";

}
Copy after login

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

}
Copy after login

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.

}
Copy after login

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' );
Copy after login

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
    
}
Copy after login

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' );
Copy after login

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:


    

Copy after login

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How long does it take to recharge digital currency to arrive? Recommended mainstream digital currency recharge platform How long does it take to recharge digital currency to arrive? Recommended mainstream digital currency recharge platform Apr 21, 2025 pm 08:00 PM

The time for recharge of digital currency varies depending on the method: 1. Bank transfer usually takes 1-3 working days; 2. Recharge of credit cards or third-party payment platforms within a few minutes to a few hours; 3. The time for recharge of digital currency transfer is usually 10 minutes to 1 hour based on the blockchain confirmation time, but it may be delayed due to factors such as network congestion.

Should Bitcoin become a medium of exchange, a medium of value storage, or both? Should Bitcoin become a medium of exchange, a medium of value storage, or both? Apr 21, 2025 pm 08:06 PM

The future of Bitcoin: a medium of exchange or a store of value? The debate continues to ferment in the core Bitcoin community, and the latest remarks by Twitter co-founder and BlockInc. CEO Jack Dorsey have sparked new discussions. Is Bitcoin a payment tool, a storage means of value, or both? This issue has always been the focus of fierce debate among industry insiders. Dorsey's recent remarks have ignited the debate again. Dispute about the use of Bitcoin Dorsey believes that if Bitcoin is stored as a store of value only, it will fail. He pointed out in a recent podcast that Bitcoin’s success lies in its payment capabilities. This view is in contrast to the current major narrative of Bitcoin. Many people, including Wink

Recommended essential software for currency contract parties Recommended essential software for currency contract parties Apr 21, 2025 pm 11:21 PM

The top ten cryptocurrency contract exchange platforms in 2025 are: 1. Binance Futures, 2. OKX Futures, 3. Gate.io, 4. Huobi Futures, 5. BitMEX, 6. Bybit, 7. Deribit, 8. Bitfinex, 9. CoinFLEX, 10. Phemex, these platforms are widely recognized for their high liquidity, diversified trading functions and strict security measures.

How to trade quantum chains How to trade quantum chains Apr 21, 2025 pm 11:42 PM

The quantum chain (Qtum) transaction process includes three stages: preliminary preparation, purchase and sale. 1. Preparation: Select a compliant exchange, register an account, perform identity verification, and set up a wallet. 2. Purchase quantum chains: recharge funds, find trading pairs, place orders (market orders or limit orders), and confirm transactions. 3. Sell quantum chains: Enter the trading page, select the trading pair and order type (market order or limit order), confirm the transaction and withdraw cash.

What is a quantum chain? What are the quantum chain transactions? What is a quantum chain? What are the quantum chain transactions? Apr 21, 2025 pm 11:51 PM

Quantum Chain (Qtum) is an open source decentralized smart contract platform and value transmission protocol. 1. Technical features: BIP-compatible POS smart contract platform, combining the advantages of Bitcoin and Ethereum, introduces off-chain factors and enhances the flexibility of consensus mechanisms. 2. Design principle: realize on-chain and off-chain data interaction through main control contracts, be compatible with different blockchain technologies, flexible consensus mechanisms, and consider industry compliance. 3. Team and Development: An international team led by Shuai Chu, 80% of the quantum coins are used in the community, and 20% rewards the team and investors. Quantum chains are traded on Binance, Gate.io, OKX, Bithumb and Matcha exchanges.

Bitcoin market trend chart in the past decade (analysis of Bitcoin price historical trends from 2014 to 2025) Bitcoin market trend chart in the past decade (analysis of Bitcoin price historical trends from 2014 to 2025) Apr 21, 2025 pm 07:30 PM

Bitcoin price has experienced multiple cycles of fluctuations and growth. 1. From 2013 to 2014, the price soared from less than $10 to $1,150, and then plummeted to $200. 2. From 2015 to 2016, prices stabilized and rebounded, and infrastructure improvements enhanced market confidence. 3. From 2017 to 2018, the price plummeted to US$3,000 after exceeding US$20,000, and the tightening of China's regulatory policies caused an impact. 4. From 2019 to 2021, the price rebounded to more than US$10,000, and exceeded US$60,000 in 2021, and institutional investors entered the market to drive the rise. 5. From 2022 to 2024, the price rebounded to $50,000 after the market correction, and the launch of US ETFs brought new funds. 6. At the beginning of 2025, the price is around 1

bitget new user registration guide 2025 bitget new user registration guide 2025 Apr 21, 2025 pm 10:09 PM

The steps to register for Bitget in 2025 include: 1. Prepare a valid email or mobile phone number and a stable network; 2. Visit the Bitget official website; 3. Enter the registration page; 4. Select the registration method; 5. Fill in the registration information; 6. Agree to the user agreement; 7. Complete verification; 8. Obtain and fill in the verification code; 9. Complete registration. After registering, it is recommended to log in to the account, perform KYC identity verification, and set up security measures to ensure the security of the account.

gate.io Android app download gate.io Android latest version download and install gate.io Android app download gate.io Android latest version download and install Apr 21, 2025 pm 07:54 PM

The steps to download the Gate.io Android APP include: 1. Visit the official website of Gate.io; 2. Select the Android version and download; 3. Download the APK file and enable the "Unknown Source" option; 4. Install the Gate.io APP. The APP provides a wealth of trading pairs, real-time market display, a variety of ordering methods, asset security, convenient asset management, and rich activities and discounts.

See all articles