Home > CMS Tutorial > WordPress > body text

Using WordPress to collect donations: Bitcoin

WBOY
Release: 2023-09-01 17:29:07
Original
1157 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!