Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

How I Built the Settings System for Easy Digital Downloads

Posted on June 20, 2012 by Pippin in Advanced, Tutorials, Video Tutorials, Writing Plugins 29 Comments
Home» Tutorials » Advanced » How I Built the Settings System for Easy Digital Downloads
Tweet
Love It - 1

The settings system that I built for Easy Digital Downloads is something I was very happy with once finished, especially because it is very extensible, and very easy to integrate with by other developers. I decided to record an overview video that walks through how the system works.

One of the primary reasons I like the system I built is because of how simple it is for add-on developers to register their own settings within EDD’s main settings tabs. Let’s take a look at the entire code that adds the settings for the Stripe Payment Gateway:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// adds the settings to the Payment Gateways section
function edds_add_settings($settings) {
 
	$stripe_settings = array(
		array(
			'id' => 'stripe_settings',
			'name' => '<strong>' . __('Stripe Settings', 'edds') . '</strong>',
			'desc' => __('Configure the Stripe settings', 'edds'),
			'type' => 'header'
		),
		array(
			'id' => 'live_secret_key',
			'name' => __('Live Secret Key', 'edds'),
			'desc' => __('Enter your live secret key, found in your Stripe Account Settins', 'edds'),
			'type' => 'text',
			'size' => 'regular'
		),
		array(
			'id' => 'live_publishable_key',
			'name' => __('Live Publishable Key', 'edds'),
			'desc' => __('Enter your live publishable key, found in your Stripe Account Settins', 'edds'),
			'type' => 'text',
			'size' => 'regular'
		),
		array(
			'id' => 'test_secret_key',
			'name' => __('Test Secret Key', 'edds'),
			'desc' => __('Enter your test secret key, found in your Stripe Account Settins', 'edds'),
			'type' => 'text',
			'size' => 'regular'
		),
		array(
			'id' => 'test_publishable_key',
			'name' => __('Test Publishable Key', 'edds'),
			'desc' => __('Enter your test publishable key, found in your Stripe Account Settins', 'edds'),
			'type' => 'text',
			'size' => 'regular'
		)	
	);
 
	return array_merge($settings, $stripe_settings);	
}
add_filter('edd_settings_gateways', 'edds_add_settings');

If you have ever setup settings pages for your own WordPress plugins, then you will immediately see how much simpler the above code is than creating an entire page with options.

One of the major benefits of the EDD settings system is that it is highly extensible.

Tweet Follow @pippinsplugins
add_settings_field, add_settings_section, easy digital downloads, register_setting, Settings API

29 comments on “How I Built the Settings System for Easy Digital Downloads”

  1. corsonr says:
    June 20, 2012 at 10:21 am

    Hi, this is a really nice way to register new settings, you did something very cool!

    Reply
    • Pippin says:
      June 20, 2012 at 11:41 am

      Thanks Remi!

  2. yellowhousedesign says:
    June 20, 2012 at 10:22 am

    Awesome tutorial Pippin. I think I’ll go through one of my plugins and re-work that area. Love that you used all the built-in styles; was thinking half-way through this — damn, that’s a lot of copying and pasting I’ll have to do from EDD :)

    Thanks again!

    Reply
    • Pippin says:
      June 20, 2012 at 11:48 am

      I really prefer to always use core UI elements, for a few of reasons:

      1. It makes the settings integrate flawlessly with WP
      2. It dramatically reduces the amount of code you have to maintain
      3. It dramatically reduces the likelihood of conflicts with other plugins/themes CSS

    • yellowhousedesign says:
      June 20, 2012 at 12:01 pm

      Completely agree. I think I was sorta on the right track with what I was doing in my plugins (as far as registering the settings in a big ‘ol array) — but using the settings API — does that take care of sanitation/validation whereas the way I did it wasn’t? Thanks!

    • Pippin says:
      June 20, 2012 at 12:03 pm

      You still have to do validation yourself with the settings API, unfortunately.

    • yellowhousedesign says:
      June 20, 2012 at 12:09 pm

      Bah okay – is that also in EDD in-case I wanted to sneak a peak at how you did it? It looks like about half-way down WPTuts covers it a bit as you mentioned http://wp.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-7-validation-sanitisation-and-input-i/

    • Pippin says:
      June 20, 2012 at 12:24 pm

      EDD has some basic sanitation at the moment, though I need to enhance it. It is handled by EDD, not by the add-on(s).

    • yellowhousedesign says:
      June 20, 2012 at 1:21 pm

      Sounds good, thanks for letting me know

  3. yellowhousedesign says:
    June 21, 2012 at 9:33 am

    Have any suggestions for creating a static tab? For example, if I wanted to have a ‘Documentation’ tab with free text. Thanks!

    Reply
    • yellowhousedesign says:
      June 21, 2012 at 10:02 am

      Think I figured this one out — I just created an output function for the ‘Documentation’ tab and appended that onto the logic for the others — seems to work great!

    • Pippin says:
      June 21, 2012 at 2:29 pm

      Yep, that’s what you need :)

  4. tareq says:
    June 22, 2012 at 6:00 am

    Few days back I wrote a similar PHP class. Check it out http://tareq.wedevs.com/2012/06/wordpress-settings-api-php-class/

    Reply
    • Pippin says:
      June 22, 2012 at 10:04 am

      That’s great! The one thing I would suggest on it is that you remove the post box styling from around the settings sections/fields, that way it looks exactly like it is meant to look in WordPress :)

  5. Steven says:
    June 25, 2012 at 1:33 pm

    Seems like a few of us had the same idea…

    I’m updating my plugin to use the Settings API and decided to make a wrapper class. I did take it a little further though. The wrapper class works using filters to register sections and settings using the Settings API. It also extends the Settings API to allow registering of tabs [optional] and the settings sections linked to the tabs It also allows me to set the position of the tab within the tab bank. Same for the settings sections.

    I also added filters to allow easy hooking into the class so I can easily add tabs/sections and position them right where where I want.

    I did opt to use three arrays for the configuration though. One for tabs, one for sections and the other for the settings all linked by and id. One large array for all three became to mind numbing to look at for me.

    So with that said it only takes one command passing two values, the page hook and the current tab [optional], to create the entire settings page.

    I’m not done yet though. Right now it just outputs everything to the screen I have no code done to actually save and update settings. But it’s coming along nicely.

    Having this will make it insanely easy to create add-ons for my plugin that can hook into its settings page.

    It sounds like a lot but it really didn’t take a whole lot of code, a few dozen lines. And since the entire back end of the class is powered by the Settings API it should be forwards compatible and make my plugin code lighter and easier to maintain.

    Reply
    • Pippin says:
      June 25, 2012 at 7:35 pm

      That’s fantastic! Do you plan to release the code, either free or paid?

  6. shazahm1 says:
    June 28, 2012 at 2:31 pm

    Well it’ll be bundle with my plugin … I didn’t give it any thought until you mentioned it, but I could throw it up on GitHub.

    I did run across a couple small road blocks. It seems the WP Settings API actually isn’t all the robust, I’m learning. Unless I’m mistaken, you can only register one settings section per page/tab so I think I’m going to have slightly re-think on how sections are ID’d because I want to be able to register multiple sections per page/tab and be able to hook into them to add additional sections if I want. Additionally, I’m working on adding set default, reset and remove methods. Seems to me the Settings API should have those, but doesn’t. At least the update_options function can be used so that should ensure forward compatibility.

    While I’m rambling …. I’m thinking of adding the option of registering contextual help screens too for settings pages. That way one could use the same array that holds the configuration for the settings fields to register a contextual help panel to go with them. Just about everything needed to do this is already in place. Really I think I only need to add the support for the title and content options for the help screen and the proper callback for the content.

    What do you think? Would that be cluttering the API?

    Reply
    • Pippin says:
      June 29, 2012 at 3:03 pm

      I don’t see any reason not to include all of those options. The point of the class or functions set that you’re writing is to make it really easy to create the settings pages, and as far as I’m concerned, that includes contextual help tabs.

  7. shazahm1 says:
    July 11, 2012 at 10:59 pm

    Just thought I’d drop a line to say that I just released the initial version of my settings API since you seemed interested if I was going to release it or not. I dropped the plans to add the support for the contextual help tabs, for now. Ran out of time. I needed to move on in the two projects that I’m going to utilize this API.

    http://connections-pro.com/2012/07/11/connections-settings-api-wrapper-class-for-wordpress/

    Available on GitHub.
    https://github.com/shazahm1/Connections-Settings-API-Wrapper-Class

    Let me know what you think.

    Reply
    • Pippin says:
      July 12, 2012 at 10:21 am

      Fantastic! I’ve left a comment with a couple of questions :)

  8. Kaike says:
    July 25, 2012 at 4:29 am

    Very nice your method. I have used similar method to themes. I have a question. What is program are you using to record screencasts on Ubuntu? Thanks and congratulations.

    Reply
    • Pippin says:
      July 25, 2012 at 9:05 am

      Thanks, I’m glad you like it. I use the app call “RecordMyDesktop”. You can install it via the Ubuntu Software Center.

  9. yellowhousedesign says:
    September 5, 2012 at 7:57 am

    Hey Pippin,
    I wanted to get your opinion on something. Using the same logic mentioned here (as far as setting up the options), I’m providing some defaults on-install like so: http://pastebin.com/mRsGpx4v The caveat I’m seeing in doing this is in the event I add-in a new option after the plugin has been installed once — the plugin then won’t update any new values I stick in there until the user re-saves the plugin options. In addition, that also makes it so I need to not only check the value of a plugin setting, but also make sure it is set in the first place. What would you recommend is the best way to handle this? I noticed you place the defaults in the plugin options themselves — but do you also run a isset() on them as well whenever they are used within the plugin — or is there a way to force a “re-save” on upgrade (not sure if there is such a function in WP) to use whatever settings are there by default? Thanks!

    Reply
    • Pippin says:
      September 5, 2012 at 1:39 pm

      I usually run an isset().

    • yellowhousedesign says:
      September 5, 2012 at 2:55 pm

      Appreciate it Pippin – will do as well

  10. Japh says:
    September 16, 2012 at 8:25 pm

    Hey Pippin, somehow I missed this when you first published it. It’s in my Readability, but I mustn’t have gone back to read/watch. Anyway, I’ve rectified that now!

    Really elegant solution here, I love to see this sort of thing :)

    Also, thanks for the Wptuts+ shout out in there. Tom has a written a few series that are really must-read reference material. This post should be too!

    Reply
    • Pippin says:
      September 16, 2012 at 8:35 pm

      Thanks for stopping by Japh :)

  11. Kyle says:
    April 1, 2013 at 7:06 am

    Hi Pippin, great tutorial,

    My question is let’s assume I want to only display settings and save specific settings for each gateway separately.

    Example: So at the top of the Gateway Tab, there would be a setting “Select your Gateway”

    When “Paypal” is selected all the settings for Paypal would show, if we change to “Stripes” then all the settings for Stripes would show. This way the options page is clean and only settings for a specific gateway would show.

    Any thoughts?

    Kyle

    Reply
    • Pippin says:
      April 1, 2013 at 11:16 am

      You will need to pull in all of the settings for the gateways section:

      $options = get_option( 'edd_settings_gateways' );
      // gateways
      $gateways = $options['gateways'];

      To update just modify the array of data and then pass it back to update_option:

      $options = get_option( 'edd_settings_gateways' );
      // tweak the options
      update_option( 'edd_settings_gateways', $options );

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Restrict Content Pro – Member Discounts for Easy Digital Downloads
  • Easy Digital Downloads – One Year Old
  • Easy Digital Downloads v1.5 Released
  • Crucial Security Flaw Discovered and Fixed
  • Easy Digital Downloads Giveaway

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • RT @jchristopher: If you run a #WordPress site &amp; want to encourage your visitors to help support efforts in Oklahoma, this should help: htt…
    May 22, 2013
  • @natkohasic I haven&#039;t tried that yet
    May 22, 2013
  • @curtismchale @Krogsgard @wpengine yes! #blamekrogs
    May 22, 2013

Topics

get_user_meta featured meta box register_setting Sugar Event Calendar attachments campaign monitor hook Rémi Corson the_content add_options_page contextual help wp_enqueue_script plugin login authors attachment forms do_action short codes image Related posts mail chimp post types apply_filters bbpress comments recent posts taxonomies short code custom post type images gallery Ajax Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins