Advertisement
  1. Code
  2. WordPress
Code

Creating a PayPal Buy Now Button With Variable Shortcodes

by
Difficulty:IntermediateLength:MediumLanguages:

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.

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.

Step 2

Next, you'll set up the plugin's options, and add an array containing the default shortcode attributes.

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.

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.

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.

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.

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.

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.

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.

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.

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').

Step 3

Then, the currency code can be set as any of the recognised PayPal currencies, such as USD or GBP.

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.

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!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.