PayPal is a great payment processor that allows anyone to send you money, which you can then send directly to your bank account. In this tutorial, you'll learn how to make a WordPress plugin allowing you to generate a Buy Now button, using a variable shortcode.
The main shortcode will have a default value of $50 USD, with the Large size, though you'll be able to overwrite this every time you enter the shortcode; this is known as a variable shortcode.
So, let's get started!
1. Initializing the Plugin
Step 1
Create a new directory in wp-content/plugins
called paypal-buy-now-button-shortcode
, and within that, create a file called paypal-buy-now-button-shortcode.php
.
Step 2
In order for WordPress to know that this is a plugin, you need to add the following header information to the beginning of your new PHP file.
<?php /** * Plugin Name: PayPal Buy Now Button Shortcode * Plugin URI: http://www.samberson.com * Description: A simple PayPal Buy Now button plugin with shortcode. * Version: 1.0.0 * Author: Sam Berson * Author URI: http://www.samberson.com * License: GPL-3.0+ * License URI: http://www.gnu.org/licenses/gpl-3.0.html * Domain Path: /lang * Text Domain: paypal-buy-now-button-shortcode */
2. Creating the Shortcode
Step 1
You'll now begin by creating a new function with attributes, called paypal_buy_now_shortcode( $atts )
, and opening up the function.
/** * Generates the markup for the shortcode on the public site. */ function paypal_buy_now_shortcode( $atts ) {
Step 2
Next, you'll set up the plugin's options, and add an array containing the default shortcode attributes.
$options = get_option( 'paypal_buy_now_options' ); $atts = shortcode_atts( array( 'amount' => '50', 'currency' => 'USD', 'size' => 'SM' ), $atts ); extract( $atts );
Step 3
You're now going to set the output for the front-end of the shortcode, which is a standard HTML form, with some PHP included in it, which will transmit the form data such as the PayPal ID, amount, currency, and button size. You'll also close off the new function, which you created before.
return '<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <div class="paypal-donations"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="' . $options['paypal_user_id'] . '"> <input type="hidden" name="amount" value="' . $amount . '"> <input type="hidden" name="rm" value="0"> <input type="hidden" name="currency_code" value="' . $currency . '"> <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_buynow_' . $size . '.gif" name="submit" alt="' . __( 'PayPal - The safer, easier way to pay online.', 'paypal-buy-now-button-shortcode' ) . '"> <img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"> </div> </form>'; }
Step 4
Finally, you'll initialise the function as a shortcode by using the add_shortcode()
function, which is built in to WordPress. Essentially, you're saying that the shortcode will be [buy_now]
, using the paypal_buy_now_shortcode
function.
add_shortcode( 'buy_now', 'paypal_buy_now_shortcode' );
3. Adding More Functions
Step 1
Next, you'll add the paypal_buy_now_user_id
function, and its options, which will allow an HTML input field to display in the back end.
/** * Generates the markup for the PayPal user id. */ function paypal_buy_now_user_id() { $options = get_option( 'paypal_buy_now_options' ); echo "<input name='paypal_buy_now_options[paypal_user_id]' type='email' value='{$options['paypal_user_id']}'/>"; }
Step 2
With the paypal_buy_now_register_settings
function, you'll be registering the settings fields in the back end, and giving their data names. In this case, you're registering the settings, adding the section to the back end, and then initializing the PayPal Id field.
/** * Registers settings page and fields. */ function paypal_buy_now_register_settings() { register_setting( 'paypal_buy_now_settings', 'paypal_buy_now_options' ); add_settings_section( 'paypal_buy_now_section', '', null, __FILE__ ); add_settings_field( 'paypal_user_id', __( 'PayPal Id', 'paypal-buy-now-button-shortcode' ),'paypal_buy_now_user_id', __FILE__, 'paypal_buy_now_section' ); }
Step 3
Similar to what you did earlier with the shortcode initialization, you're going to add the paypal_buy_now_register_settings
function as an action in the admin area.
add_action( 'admin_init', 'paypal_buy_now_register_settings' );
4. Admin Options HTML
Step 1
The paypal_buy_now_options_page
function will allow you to add the HTML required to display the form correctly in the back end. After adding the function, you'll create the HTML structure, with an opening div
and h2
, and then the form with the fields you initialized earlier.
/** * Generates the markup for the options page. */ function paypal_buy_now_options_page() { ?> <div class="wrap"> <h2><?php _e( 'PayPal Buy Now Shortcode Settings', 'paypal-buy-now-button-shortcode' ); ?></h2> <form method="post" action="options.php" enctype="multipart/form-data"> <?php settings_fields( 'paypal_buy_now_settings' ); do_settings_sections( __FILE__ ); ?> <p class="submit"> <input type="submit" class="button-primary" name="submit" value="<?php _e( 'Save Changes', 'paypal-buy-now-button-shortcode' ); ?>"> </p> </form> </div> <?php }
Step 2
Now, the paypal_buy_now_add_settings_menu
function will allow you to create a new menu / page in the back end, and define what the various titles should be.
/** * Adds menu item to the settings. */ function paypal_buy_now_add_settings_menu() { $title = __( 'PayPal Buy Now Shortcode', 'paypal-buy-now-button-shortcode' ); add_options_page( $title, $title, 'administrator', __FILE__, 'paypal_buy_now_options_page'); } add_action( 'admin_menu', 'paypal_buy_now_add_settings_menu' );
5. Using the Plugin
You've now successfully created the plugin, but how do you use it? Well, it's very simple.
Step 1
If you use the main shortcode on its own, like below, you'll see a large button, which when clicked will set the amount as $50 USD.
[buy_now]
Step 2
You can then use the different attributes you set up to alter the button. So you can set the size to either small ('SM'), or large ('LG').
[buy_now size=LG]
Step 3
Then, the currency code can be set as any of the recognised PayPal currencies, such as USD or GBP.
[buy_now currency=GBP size=LG]
Step 4
Finally, you'll set the amount as a numerical value, such as 100, or 40. Remember not to include the currency symbol in the amount variable.
[buy_now amount=150 currency=GBP size=LG]
Step 5
One final point I should make is that because there are default values set already, you don't need to set all three variables each time; you could simply change one or two of them, if need be.
In Summary
That's it! You've now created a new plugin which allows you to create a PayPal Buy Now button, using a variable shortcode. If you have any questions, please feel free to leave a comment below!
Comments