Couple of months ago, I was looking for a simple WordPress plugin to use on one of my client’s website to accept PayPal donations. I was looking for an easy and flexible way to add a PayPal donation button in posts content without hardcoding links everywhere. There are a couple of interesting plugins I found but they had tons of features which my client did not require and for such a simple feature, I did not want to bloat his website. So I started digging more into it and trying to find a simple way to add PayPal donation button without using any plugins and the best suggestion I get is to use a shortcode to create PayPal donation button. So in this article I will show you how you can create a simple shortcode for adding a PayPal donation button in your WordPress posts, sidebar, or anywhere else without a plugin. Previously we have also posted about using shortcodes for protecting email address and hiding content from non members.
If you feel comfortable using a plugin then you can search WordPress plugin repository for a suitable plugin for your requirements but if you want to achieve this with couple of lines of codes then here’s a simple function for creating a shortcode that can be used on a WordPress blog to render a text donation link that will redirect to PayPal.
All you have to do is copy the following WordPress code snippet in your functions.php file and you can use the shortcode in your post/page content. Don’t forget to change PayPal account in following snippet.
// add shortcode for paypal donation button in wordpress
function wcs_paypal_donate_shortcode( $atts ) {
extract( shortcode_atts( array(
'text' => 'Make a donation',
'account' => 'paypal-account',
'for' => '',
), $atts ) );
global $post;
if ( !$for ) $for = str_replace( " ", "+", $post->post_title );
return '<a class="paypal_donate" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" . $account . "&item_name=Donation+for+' . $for . '">' . $text . '</a>';
}
add_shortcode( 'paypal', 'wcs_paypal_donate_shortcode' );
To use this shortocde you can use following formats in your content.
[paypal]
[paypal account="new-paypal-account" text="Buy me a coffee!"]