Skip to content
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 4adec38a530780be5661bd11f8a9bf9e81b63ea9 @tommcfarlin committed Feb 9, 2012
Showing with 189 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +130 −0 functions.php
  3. +29 −0 index.php
  4. +29 −0 style.css
1 .gitignore
@@ -0,0 +1 @@
+.DS_Store
130 functions.php
@@ -0,0 +1,130 @@
+<?php
+
+/* ------------------------------------------------------------------------ *
+ * Setting Registration
+ * ------------------------------------------------------------------------ */
+
+/**
+ * Initializes the theme's options page by registering the Sections,
+ * Fields, and Settings.
+ *
+ * This function is registered with the 'admin_init' hook.
+ */
+add_action('admin_init', 'sandbox_initialize_theme_options');
+function sandbox_initialize_theme_options() {
+
+ // First, we register a section. This is necessary since all future options must belong to a
+ add_settings_section(
+ 'general_settings_section', // ID used to identify this section and with which to register options
+ 'Sandbox Options', // Title to be displayed on the administration page
+ 'sandbox_general_options_callback', // Callback used to render the description of the section
+ 'general' // Page on which to add this section of options
+ );
+
+ // Next, we'll introduce the fields for toggling the visibility of content elements.
+ add_settings_field(
+ 'show_header', // ID used to identify the field throughout the theme
+ 'Header', // The label to the left of the option interface element
+ 'sandbox_toggle_header_callback', // The name of the function responsible for rendering the option interface
+ 'general', // The page on which this option will be displayed
+ 'general_settings_section', // The name of the section to which this field belongs
+ array( // The array of arguments to pass to the callback. In this case, just a description.
+ 'Activate this setting to display the header.'
+ )
+ );
+
+ add_settings_field(
+ 'show_content',
+ 'Content',
+ 'sandbox_toggle_content_callback',
+ 'general',
+ 'general_settings_section',
+ array(
+ 'Activate this setting to display the content.'
+ )
+ );
+
+ add_settings_field(
+ 'show_footer',
+ 'Footer',
+ 'sandbox_toggle_footer_callback',
+ 'general',
+ 'general_settings_section',
+ array(
+ 'Activate this setting to display the footer.'
+ )
+ );
+
+ // Finally, we register the fields with WordPress
+ register_setting(
+ 'general',
+ 'show_header'
+ );
+
+ register_setting(
+ 'general',
+ 'show_content'
+ );
+
+ register_setting(
+ 'general',
+ 'show_footer'
+ );
+
+} // end sandbox_initialize_theme_options
+
+/* ------------------------------------------------------------------------ *
+ * Section Callbacks
+ * ------------------------------------------------------------------------ */
+
+/**
+ * This function provides a simple description for the General Options page.
+ *
+ * It's called from the 'sandbox_initialize_theme_options' function by being passed as a parameter
+ * in the add_settings_section function.
+ */
+function sandbox_general_options_callback() {
+ echo '<p>Select which areas of content you wish to display.</p>';
+} // end sandbox_general_options_callback
+
+/* ------------------------------------------------------------------------ *
+ * Field Callbacks
+ * ------------------------------------------------------------------------ */
+
+/**
+ * This function renders the interface elements for toggling the visibility of the header element.
+ *
+ * It accepts an array or arguments and expects the first element in the array to be the description
+ * to be displayed next to the checkbox.
+ */
+function sandbox_toggle_header_callback($args) {
+
+ // Note the ID and the name attribute of the element match that of the ID in the call to add_settings_field
+ $html = '<input type="checkbox" id="show_header" name="show_header" value="1" ' . checked(1, get_option('show_header'), false) . '/>';
+
+ // Here, we'll take the first argument of the array and add it to a label next to the checkbox
+ $html .= '<label for="show_header">&nbsp;' . $args[0] . '</label>';
+
+ echo $html;
+
+} // end sandbox_toggle_header_callback
+
+function sandbox_toggle_content_callback($args) {
+
+ $html = '<input type="checkbox" id="show_content" name="show_content" value="1" ' . checked(1, get_option('show_content'), false) . '/>';
+ $html .= '<label for="show_content">&nbsp;' . $args[0] . '</label>';
+
+ echo $html;
+
+} // end sandbox_toggle_content_callback
+
+function sandbox_toggle_footer_callback($args) {
+
+ $html = '<input type="checkbox" id="show_footer" name="show_footer" value="1" ' . checked(1, get_option('show_footer'), false) . '/>';
+ $html .= '<label for="show_footer">&nbsp;' . $args[0] . '</label>';
+
+ echo $html;
+
+} // end sandbox_toggle_footer_callback
+
+?>
29 index.php
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>The Complete Guide To The Settings API | Sandbox Theme</title>
+ </head>
+ <body>
+
+ <?php if(get_option('show_header')) { ?>
+ <div id="header">
+ <h1>Sandbox Header</h1>
+ </div><!-- /#header -->
+ <?php } // end if ?>
+
+ <?php if(get_option('show_content')) { ?>
+ <div id="content">
+ <p>This is theme content.</p>
+ </div><!-- /#content -->
+ <?php } // end if ?>
+
+ <?php if(get_option('show_footer')) { ?>
+ <div id="footer">
+ <p>&copy; <?php echo date('Y'); ?> All Rights Reserved.</p>
+ </div><!-- /#footer -->
+ <?php } // end if ?>
+
+ </body>
+</html>
+
+<!-- Note that the above markup is extraordinarily simple and I do not recommend using this as a foundation for theme development. It's simply providing the means by which we will be reading values from the Settings API. -->
29 style.css
@@ -0,0 +1,29 @@
+/*
+Theme Name: WordPress Settings Sandbox
+Theme URI: http://github.com/tommcfarlin/wordpress-settings-sandbox
+Description: A simple theme used to showcase the WordPress Settings API. The corresponding series can be found on <a href="http://wp.tutsplus.com/series/the-complete-guide-to-the-wordpress-settings-api/">Envato</a>.
+Author: Tom McFarlin
+Author URI: http://tommcfarlin.com
+Version: 0.1
+
+License:
+
+ Copyright 2012 Tom McFarlin (tom@tommcfarlin.com)
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License, version 2, as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+The Complete Guide To The WordPress Settings API
+http://wp.tutsplus.com/series/the-complete-guide-to-the-wordpress-settings-api/
+
+*/

0 comments on commit 4adec38

Please sign in to comment.
Something went wrong with that request. Please try again.