Skip to content

Integrating Payments into WordPress, Part 3: Build Your Own PayPal Plugin

This is the third and final article in my series on using the WordPress weblog and CMS system.

In the first article I introduced WordPress (aka “WP”), provided links to documentation to help you get up to speed with WP, and then showed you how to install and start using it. In the second installment, I provided an overview of WP plugins, made some general plugin recommendations, and showed you how to search for PayPal-related plugins in the WordPress Plugin Directory. I also demonstrated two popular PayPal plugins, one a shopping cart, the other a plugin to accept donations.

This time I want to detail the WordPress plugin model. We’ll get our hands dirty with some code for an example WordPress plugin. And by the end, you’ll know how to go about creating a simple WP plugin of your own.

A note about PHP

In order to hack around in WordPress, and especially to modify or write new WP plugins, you will need to have some PHP programming knowledge.

The good news is that PHP is a relatively straightforward language. There are also some very nice resources for getting up to speed quickly with it.

If you just want a minimalist, WP-oriented introduction to PHP, I’d recommend you work through Adam Brown‘s “PHP Tutorial for WordPress Users“. If you want to go further, dive into the official PHP manual and tutorial. Those resources should give you the prerequisite PHP knowledge to work through the rest of this article.

Understanding The Loop

There’s one more major item you need to understand before we dive into WP plugin code. That item lies at the core of how WP renders post contents. It is The Loop.

The WP Codex summarizes The Loop like this:

The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post. When WordPress documentation states “This tag must be within The Loop”, such as for specific Template Tag or plugins, the tag will be repeated for each post.

Some plugins operate within The Loop, others outside of it. As you design a plugin, keep The Loop in mind and make sure that if your plugin will be manipulating post contents display that it’s setup to be executed within the cycles of The Loop. If you have any questions about what The Loop is or how to interact with it, I would encourage you to take your time reading through its Codex page and the resources linked to from it including “The Loop in Action“.

Note that the “The” is always capitalized in “The Loop” because of its great importance in the WP system. A bit high and mighty I guess, but just go with it.

The anatomy of a WordPress plugin

It’s time to start looking at plugin code. The Codex page “Writing a Plugin” provides a high level overview of the steps required and where to go to learn more about each. The first few steps you need to take are to create:

  1. A unique plugin name; you should search through the Plugin Directory to make certain your name is unique so that you can avoid naming collisions later
  2. One or more plugin files; you must have at least one PHP file containing your plugin code but you may have additional files (JavaScript, CSS, images, etc.)
  3. Optional: A readme.txt to provide more information if you list your plugin in the Plugin Directory; the format for your readme is described here
  4. Optional: A homepage for your plugin if you are going to distribute it to others; this page should describe what the plugin does, how to install it, version compatibility and prerequisites, how to use the plugin, etc. and you may either use your Plugin Directory page or create a page on your own site for this

Let’s look at an example plugin. We’re going to examine the first plugin ever written for WP, “Hello Dolly“. It is the oldest WP plugin, created by the founding developer of WP. It is also a very good plugin to examine because it’s simple and it’s installed with WP installations by default. If you’re following along with this article series and have installed WP, you should already have a copy of “Hello Dolly” on your server. If for some reason you don’t see it listed on your server’s plugin administration page (http://yourserver/wp-admin/plugins.php) you can install it from its Plugin Directory page (click here).

“Hello Dolly” consists of only one PHP file, hello.php, contained within a hello-dolly directory in the downloadable ZIP distribution.

At the top of the Hello Dolly source code listing you see some standard plugin information. This plugin info header lets WP recognize the plugin, add it to the plugin administration screens, and allow you to activate/deactivate it on your server. The header format is detailed on “Writing a Plugin” as:

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
?>

Note that at least Plugin Name must be provided. Some plugins may omit one or more of the other header lines. Hello Dolly, for instance, does not provide a License line.

Next in the source you’ll see the lyrics to the song “Hello, Dolly!” and some code that splits the lyrics up into lines based upon newline characters. The code then randomly chooses a line to display using the code below.

After the lyric logic, we get into the meat of the plugin, the hello_dolly and dolly_css functions. These two functions hook into the WP system via action “hooks” that set those functions up to execute when certain actions occur within the WP system. I discussed the WP Plugin API’s action and filter hooks in the previous article in this series; please refer to that article if you need a refresher on WP hooks.

For our purposes here, the key point is that hello_dolly hooks into WP via the admin_footer action (executed at the end of the administration panel) while dolly_css hooks in via admin_head (executed in the HTML head section of the admin interface).

In other words, each of these functions is registered as a callback such that when the specified administrative interface action occurs, they are executed and the previously chosen line from the song is displayed in the upper right portion of the WP admin interface.

Wrap your brain around this critical callback functionality and WP plugin programming becomes fairly straightforward.

By the way, the same gentleman that provides the “PHP Tutorial for WordPress Users” discussed previously also maintains a very useful “WordPress Hooks Database“. Highly recommended for learning about any and all of the large number of action and filter hooks built into WP and available for your use.

Click here to read the complete article on the PayPal X Developer Network including how to build, deploy, and test an example WP plugin that fetches a PayPal account balance.

PayPal's very smart purchase of FigCard

PayPal recently announced their purchase of FigCard, makers of an emerging mobile payments system.

The official PayPal blog post on the purchase noted:

Fig Card developed an extremely easy way for merchants to accept mobile payments in stores by using a simple and very low cost USB device that plugs into the cash register or point-of-sale terminal. All the consumer needs is the Fig app on his or her smart phone.

The background on the FigCard site is even more intriguing:

A while back, we wondered why we weren’t using our fancy iPhones to pay for things. Why do we have to take our iPhone and our wallet to the gym? Why do we have to carry supermarket cards, drug store cards and lunch loyalty cards around? Obviously the phone can do all of that. It’s not that there haven’t been other attempts at solving this problem. But clearly they haven’t worked, and there’s usually more than one reason. Our patent pending solution requires no new hardware on the phones (iPhone, Android, many Blackberry models) and free hardware for the merchants. Transactions complete faster than credit card and often faster than cash.

The best way to quickly grok the potential of their system, including how it could possibly be faster than a cash payment (think “preloading”) is to watch a demonstration from FigCard co-founder Max Metral.  Watch it below or click here if the embedded video isn’t working properly.

Click here to read the complete post on the PayPal X Developer Network including information on the key points called out in the demo video and why I think this holds promise for PayPal X Platform based development.

Notes from the week of 2011-05-08

PayPal X Platform

APIs and development

Personal things

Running

Android Developer Challenge deadline May 14th

I last wrote about PayPal’s third Developer Challenge in March.  The Challenge is focused on Android application development and offers some nice prize money for the top three applications, with $25,000 USD up for grabs for first place.  And all three of the top finishers get PayPal marketing and PR assistance to help them make their app a success in the market, too.

For more details, please refer to my March post, “Android Apps for money and fame“.

First deadline is May 14th

The first major deadline is fast approaching.  You must submit your application to PayPal for approval by May 14th if you are going to participate in the challenge.  As PayPal said in a recent email to PayPal X Developer Network community members:

If you haven’t registered to get in on the PayPal X Developer Challenge for Android, you could miss out on prizes of up to $25,000, and industry buzz for your app. Registration is closing on May 14, 2011, so be sure to register now at x.com/devchallenge

Click here to read the complete post on the PayPal X Developer Network.

Too many April showers

We’ve had some pretty serious April showers in my part of the US and they didn’t bring May flowers, but rather flooding.  I myself am high and dry, but many others aren’t so lucky.  I hope this monthly update finds you well wherever you are.

As for PayPal, payments, and developer highlights from last month, I have much to report.

April was a banner month for me on the article front.  I completed my “Selling Digital Goods in Data Markets” series.  Part three showed you how to build an example data market using Python and Google App Engine.  Part four picked up where the previous part left off, examining what you would need to do to implement a RESTful web API for your data market while allowing finer grained, single row or entity level data sales using micropayments.  Click here to access all four articles in the series.

https://www.x.com/servlet/JiveServlet/downloadBody/3392-102-2-3805/example_example_dataset.png

After completing the data markets articles, I began a new series on using PayPal and WordPress.  “Integrating Payments into WordPress, Part 1:  Features and Getting Started” introduced the WordPress system, how to install and begin using it, and where to go to learn more as you do so.  “Integrating Payments into WordPress, Part 2:  PayPal Plugins” introduced WordPress plugins, made some recommendations on popular plugins you might wish to try, and looked at existing PayPal plugin options.  The remaining article in this series will detail how to start building your own WordPress plugins using a PayPal-related example; watch for this to publish soon.

https://www.x.com/servlet/JiveServlet/downloadBody/3463-102-1-3858/20110413_article_001_pt1sm.jpg

I also wrote the following DevZone blog posts last month:

Click to read the complete post on the PayPal X Developer Network including links to other important developer news and information from April.

Notes from the week of 2011-05-01

PayPal X Platform

Wireless and mobility

APIs and development

Personal things

  • It's nice to see Google buying wind energy for its new Oklahoma data center http://bit.ly/dUmns1 but I wonder about transmission efficiency #

Running

WP Plugin Directory search

Who will corner the market on mobile card readers?

Mobile credit card reader competition continues to heat up.

Square credit card readers are now being sold directly by Apple in their retail stores and online.  Square readers also work on iPad and iPod touch (i.e. any current iOS-based device) as well as Android devices.  The readers connect using a mobile device’s headphone jack.

Square in the iTunes App Store

It’s interesting to note that if you sign up for Square’s service directly via their own site, you get the reader mailed to you for free.  If you buy the reader from Apple, it costs $9.95 which Square then offsets for you with a $10 credit to your merchant account.

However you acquire your card reader, Square’s giving you the reader “razor” to be able to sell card swipe fee “blades” repeatedly.  Note that you can also use Square’s iOS or Android app to process transactions by keying in details rather than swiping a card.  This changes the fees a bit (see table below for details).  Either way, you get a free reader for what Square hopes will be many, many transaction fees in return.

There have been some previous allegations that Square’s system wasn’t secure, but they received a huge statement of confidence this week with the announcement that Visa has made a strategic investment in Square.  I’d be very surprised if Visa hadn’t done their due diligence on Square’s system before making such an investment and announcing it to the world.  You can follow the latest news on this investment via @Square on Twitter.

Click to read the complete post on the PayPal X Developer Network including information on how Square compares to their major competitor, Intuit GoPayment. The post includes a table summarizing the key fees of both systems.

Integrating Payments into WordPress, Part 2: PayPal Plugins

In my previous article I introduced WordPress (aka “WP”), provided links to documentation to help you get up to speed with WP, and then showed you how to install and start using it.

This time I want to describe the WordPress plugin model. I’ll make some recommendations for plugins that are not only useful, but also instructive as you learn about what’s possible in WP. Then I’ll examine some of the most popular and highest rated PayPal plugins according to WP users. I’ll also identify payments functionality that might be missing or lacking in currently available plugins; we’ll go deeper into that functionality in a future article.

Extending WordPress with plugins

As the WordPress Codex succinctly puts it:

Plugins are tools to extend the functionality of WordPress

That’s a high level definition for WP users, but what about a workable definition for programmers and hackers? Here’s a better description from the Codex “Writing a Plugin” page:

A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API).

The Plugin API page goes on to provide a lot of useful information on the hooks you can use to tie plugins into WordPress. These hooks falls into two basic categories, Actions and Filters. In a nutshell, actions are event based hooks (WP launches the appropriate plugin when a specific action occurs) whereas filters are text modification hooks (WP invokes these to modify text before writing the modified text to the WP database or sending it to a client browser).

Understanding how to hook a new plugin into WordPress is key for any developer interested in extending the WP system with their own functionality. The above links provide enough information for you to explore this further on your own, plus in the next installment of this series we’ll dive deeper into plugin development together. Before we do that, however, let’s look at some popular plugins, both general purpose and PayPal-related options, so you have a good understanding of what’s available and what those plugins can do.

Plugin recommendations

The definitive place to search for existing plugins is the WordPress Plugin Directory.

As of this writing, the directory contains 14,084 plugins. The directory allows you to search for plugins to meet whatever need you have for extending WP. It allows you to filter plugin search results based upon relevance, how recently plugins were added to the directory, how recently they were updated in the directory, how many WP users have downloaded them (popularity), and how highly users have rated them.

To give you a better feel for the sort of things that plugins might provide, I’ve listed a few of my favorite WP plugins below. I use these on BillDay.com and recommend them to others looking for similar functionality for their WP-powered sites. Click on the plugin name below to visit that plugin’s page in the directory to learn more about it or install it yourself.

  • Advertising Manager – makes it easier to administer Google AdSense and other advertising and referral blocks
  • Akismet – protects your blog from comment and trackback spam by connecting to the Akismet server and comparing with spam contributions from millions of other users (requires free Akistmet key to activate)
  • FeedBurner FeedSmith – redirects your WP feeds to a FeedBurner feed so you can learn about your subscribers (link not currently available; I’m not sure if this has been updated under a different name or not)
  • Feed Stats for WordPress – easy way to view statistics from your FeedBurner feed in the WP Dashboard
  • Google Analytics Dashboard – shows a summary of your Google Analytics tracked activity in the WP Dashboard
  • Google Analytics for WordPress – makes it easy for you to add Google Analytics tracking to your WP-powered site
  • PostRank – makes it easier for you to quickly see which of your WP posts are most popular with readers, track social media analytics, and engage with site visitors from your WP Dashboard
  • ShareThis – empowers your visitors to share a post or page using e-mail or posting to a great many social networks
  • Twitter Tools – integrates your WordPress blog and Twitter, allowing you to import tweets into WP and/or send blog posts to Twitter; you can also optionally show your tweets in your WP sidebar, and post tweets from the WP administrative interface
  • WordPress Hashcash – JavaScript which blocks spam bots

PayPal plugins

Now that you know what plugins are and how to find them in the WP directory, let’s search for PayPal related plugins to see what’s currently available.

Simply doing a naïve search for all possible related plugins using the term “PayPal” yields 313 plugins in the results. That’s a few too many to discuss each in this article!

Let’s limit the search more by changing the search term to be “PayPal shopping cart“. That gives us a much more manageable nine plugins to consider. From there we can sort by popularity and user rating.

From these searches we see that “WordPress Simple Paypal Shopping Cart” is the highest rated and the second most popular PayPal shopping cart plugin. You would click on the plugin title to examine its page and then download it if you’re interested in installing it.

Nate Sanders from the PayPal team (@ppnsanders) wrote a detailed blog post on this plugin several months ago. Click here to read Nate’s post including screenshots and details on how to use the plugin once you have it installed.

Click here to read the complete post on the PayPal X Developer Network including a look at how to accept donations using a WordPress plugin.

PayPal Donations plugin

PayPal and WordPress

I’m exploring WordPress features including its plugin model in a series of DevZone articles publishing now (click here to read part 1 and here for part 2).

https://www.x.com/servlet/JiveServlet/downloadBody/3476-102-1-3872/20110419_005_pt2sm.jpg

As I’m developing the third part of this series, I’d like to ask for your input.  I’ve put together a short three question survey that will help me better understand how many readers are using WordPress, and of those readers, the sort of plugins they’re currently using as well as what payments related plugin they might like to see developed as part of the article series.

Click here to read the complete post on the PayPal X Developer Network or take the survey directly via the form below.

Integrating Payments into WordPress, Part 1: Features and Getting Started

I’ve explored a number of ways to integrate the power of the PayPal X Platform into your applications in my DevZone articles over the last few months. Recent installments have shown you how to accelerate your Adaptive Payments development using the Apigee PayPal API Console and how to buy and sell digital goods in data markets using PayPal Embedded Payments and micropayments.

Now I’d like to explore how to use PayPal payment solutions in the world’s most popular blog and content management system (CMS) software: WordPress.

In this article I’ll introduce WordPress (commonly abbreviated as “WP” by WordPress developers and power users, aka “Pressers“), then take you on a whirlwind tour through installing and using it. Future articles will introduce the WordPress plugin model through which you can greatly extend the capabilities of your WP-powered system. They will also discuss PayPal-based WP plugins and how to write your own payments plugins.

What is WordPress?

WordPress.org, the central WP development and Presser community hub, describes it thusly:

WordPress is web software you can use to create a beautiful website or blog

In essence, WordPress is a freely available, open source, PHP and MySQL based web publishing system. It’s very customizable out of the box, and can be further extended as needed through a little PHP hackery. The WP homepage goes on to note:

The core software is built by hundreds of community volunteers, and when you’re ready for more there are thousands of plugins and themes available to transform your site into almost anything you can imagine. Over 25 million people have chosen WordPress to power the place on the web they call “home”

You can get a feel for what’s possible by visiting the WordPress Showcase page and looking through a few of the thousands of sites powered by the software. Click here to check out the Showcase. Go ahead, I’ll wait.

Pretty impressive, right? Now that you have a feel for what’s possible, let’s examine how to implement a site using WP.

WordPress documentation

The first thing you should do is to visit the WordPress Codex, source of official documentation for the software, its installation and usage, and how to develop WP add-ons such as plugins and themes. The Codex contains the following sections:

  • Getting Started with WordPress – where to start, how to install the software, how to publish your first posts, file and plugin management, a “New to WordPress” FAQ, and a section on the latest release family
  • Working with WordPress – contains a lot of useful information on WP features and how to use them, theme and plugin administration, creating backups, fighting comment spam, and much more; plan to spend some time here if you’re serious about getting the most out of WP
  • Design and Layout – dives into blog design and layout issues with a lot of focus paid to CSS, themes, and templates (all used extensively by WP)
  • Advanced Topics – information on multiuser WP, deeper details on important WP concepts such as “The Loop” that displays each post, and tips for optimizing your installation
  • Troubleshooting – FAQs galore and how to find answers when you’re having problems
  • Developer Documentation – database details, discussion of the plugin API, WP cookies and coding standards, and how to use PHP to bend WP to your will; we’ll be discussing this information in more detail throughout this series
  • About WordPress – WP contributor acknowledgements, version information, WP community terminology, how to contribute to WP, documentation and Codex information, etc.

There are a couple of pages linked to from the sections above that I want to specifically call to your attention now, before we dive into installation, usage, and plugin extensions.

The first is the list of WP features (click here to access). If you have a question about whether or not WP can do something, this is a good place to start your search for the answer. The features page also lists the currently required prerequisite software including release numbers (as of this writing, PHP 4.3 or newer and MySQL 4.1.2 or newer, but these are shifting targets as new releases are made). Note that you’ll also need a web server that supports PHP and MySQL (I prefer Apache and will use it for the rest of this series).

In addition to the features and prerequisites, you might also want to spend a little bit of time at this point reading up on WP terminology and jargon (click here to access). Grounding yourself in the language used by Pressers will help to accelerate your learning process and remove unnecessary barriers as you move along.

Installing WordPress

Enough talk, let’s install! WP is famous for its easy installation. In fact, many web hosting companies have easy installation processes that will automatically install WP for you with minimal muss. If your host offers this, I’d suggest trying it.

If your host doesn’t provide a WordPress auto-installer, then you can follow the steps in the “Installing WordPress” Codex page. In brief, you need to:

  1. Download and unzip the WordPress package (click here for the latest stable release)
  2. Create a MySQL user and database for use by your WP installation
  3. Rename the WP configuration file, wp-config-sample.php, to wp-config.php so you can begin editing it (you might even want to copy it to the new filename rather than renaming it, that way you can reference the initial contents of the file after you make changes to the copied one)
  4. Edit wp-config.php in a text editor to configure the database access information you created in the second step above
  5. Place the WP files where you want them on your web server (putting them in the root of your domain has its advantages, but there may be reasons for putting them in a subirectory of your site; whichever you choose, note down the location now as you’ll need it later)
  6. Remember how I told you that you needed to know where you put your WP files? You’ll use that here. You need to execute the WP installation script by accessing wp-admin/install.php in a web browser. If you installed WP in a subdirectory on the example.com domain, for instance, you could access the script at http://example.com/subdirectory/wp-admin/install.php; if you installed WP to the example.com domain root, the script would be at http://example.com/wp-admin/install.php

Additional installation details are available from the “Installing WordPress” page.

First steps after installation

Once you’ve installed WP, you’re ready to take it for a spin. The Codex provides a great tutorial for your first time, aptly named “First Steps with WordPress“.

This tutorial will walk you through the basics of logging in to the WP administration interface and performing various admin actions. This includes a look at the WP dashboard which summarizes key information when you first login. For instance, here’s my own site’s dashboard (note that I use several plugins such as the Google Analytics plugin displaying key metrics in my dashboard as pictured here; I’ll discuss these plugins in the next article in this series).

The tutorial goes on to illustrate how to change the look of your content using themes (click here to dive deeper into themes and here for more on WP’s use of CSS for styling). It also details how to write and publish posts, how to manage comments that your site visitors (readers) make on those posts, and much more.

I strongly encourage you to spend some time working your way through “First Steps with WordPress“.

Click here to read the complete post on the PayPal X Developer Network including a bit more about the PayPal WP plugins to be discussed in the next article in the series.

Design a site like this with WordPress.com
Get started