Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | iacchi | 1 | <?php |
2 | /* |
||
3 | * Theme Options |
||
4 | */ |
||
5 | |||
6 | /* |
||
7 | * Register the form setting for our sdt_options array. |
||
8 | * |
||
9 | * This function is attached to the admin_init action hook. |
||
10 | * |
||
11 | * This call to register_setting() registers a validation callback, sdt_theme_options_validate(), |
||
12 | * which is used when the option is saved, to ensure that our option values are complete, properly |
||
13 | * formatted, and safe. |
||
14 | * |
||
15 | * We also use this function to add our theme option if it doesn't already exist. |
||
16 | */ |
||
17 | function sdt_theme_options_init() { |
||
18 | |||
19 | // If we have no options in the database, let's add them now. |
||
20 | if (false === sdt_get_theme_options()) |
||
21 | add_option('sdt_theme_options', sdt_get_default_theme_options()); |
||
22 | |||
23 | register_setting( |
||
24 | 'sdt_options', // Options group, see settings_fields() call in theme_options_render_page() |
||
25 | 'sdt_theme_options', // Database option, see sdt_get_theme_options() |
||
26 | 'sdt_theme_options_validate' // The sanitization callback, see sdt_theme_options_validate() |
||
27 | ); |
||
28 | } |
||
29 | add_action('admin_init', 'sdt_theme_options_init'); |
||
30 | |||
31 | /* |
||
32 | * Change the capability required to save the 'sdt_options' options group. |
||
33 | * |
||
34 | * @see sdt_theme_options_init() First parameter to register_setting() is the name of the options group. |
||
35 | * @see sdt_theme_options_add_page() The edit_theme_options capability is used for viewing the page. |
||
36 | * |
||
37 | * By default, the options groups for all registered settings require the manage_options capability. |
||
38 | * This filter is required to change our theme options page to edit_theme_options instead. |
||
39 | * By default, only administrators have either of these capabilities, but the desire here is |
||
40 | * to allow for finer-grained control for roles and users. |
||
41 | * |
||
42 | * @param string $capability The capability used for the page, which is manage_options by default. |
||
43 | * @return string The capability to actually use. |
||
44 | */ |
||
45 | function sdt_option_page_capability($capability) { |
||
46 | return 'edit_theme_options'; |
||
47 | } |
||
48 | add_filter('option_page_capability_sdt_options', 'sdt_option_page_capability'); |
||
49 | |||
50 | /* |
||
51 | * Add our theme options page to the admin menu, including some help documentation. |
||
52 | * |
||
53 | * This function is attached to the admin_menu action hook. |
||
54 | */ |
||
55 | function sdt_theme_options_add_page() { |
||
56 | $theme_page = add_theme_page( |
||
57 | __('Theme Options', 'sdt'), // Name of page |
||
58 | __('Theme Options', 'sdt'), // Label in menu |
||
59 | 'edit_theme_options', // Capability required |
||
60 | 'theme_options', // Menu slug, used to uniquely identify the page |
||
61 | 'sdt_theme_options_render_page' // Function that renders the options page |
||
62 | ); |
||
63 | |||
64 | if (!$theme_page) |
||
65 | return; |
||
66 | |||
67 | $help = '<p>'.__('Some themes provide customization options that are grouped together on a Theme Options screen. If you change themes, options may change or disappear, as they are theme-specific. Your current theme, Simple Dark Theme, provides the following Theme Options:', 'sdt').'</p>' . |
||
68 | '<ol>' . |
||
69 | '<li>'.__( '<strong>Zenphoto integration</strong>: You can insert the URL of your Zenphoto installation (if on the same domain of this WordPress installation) to integrate it on a WordPress page.', 'sdt').'</li>'. |
||
70 | '</ol>'. |
||
71 | '<p>'.__('Remember to click "Save Changes" to save any changes you have made to the theme options.', 'sdt').'</p>'. |
||
72 | '<p><strong>'.__('For more information:', 'sdt').'</strong></p>'. |
||
73 | '<p>'.__('<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'sdt').'</p>'. |
||
74 | '<p>'.__('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'sdt').'</p>'; |
||
75 | |||
76 | add_contextual_help($theme_page, $help); |
||
77 | } |
||
78 | add_action('admin_menu', 'sdt_theme_options_add_page'); |
||
79 | |||
80 | /* |
||
81 | * Returns the default options |
||
82 | */ |
||
83 | function sdt_get_default_theme_options() { |
||
84 | $default_theme_options = array( |
||
85 | 'zenphoto_url' => 'http://' |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | /* |
||
90 | * Returns the options array |
||
91 | */ |
||
92 | function sdt_get_theme_options() { |
||
93 | return get_option('sdt_theme_options', sdt_get_default_theme_options()); |
||
94 | } |
||
95 | |||
96 | /* |
||
97 | * Returns the options page |
||
98 | */ |
||
99 | function sdt_theme_options_render_page() { |
||
100 | ?> |
||
101 | <div class="wrap"> |
||
102 | <?php screen_icon(); ?> |
||
103 | <h2><?php printf(__('%s Theme Options', 'sdt'), get_current_theme()); ?></h2> |
||
104 | <?php settings_errors(); ?> |
||
105 | |||
106 | <form method="post" action="options.php" style="margin-top: 20px;"> |
||
107 | <?php |
||
108 | settings_fields('sdt_options'); |
||
109 | $options = sdt_get_theme_options(); |
||
110 | $default_options = sdt_get_default_theme_options(); |
||
111 | ?> |
||
112 | <fieldset> |
||
113 | <label><?php _e('Zenphoto URL:', 'sdt'); ?></label> |
||
114 | <legend class="screen-reader-text"><span><?php _e('Zenphoto URL', 'sdt'); ?></span></legend> |
||
115 | <input type="text" name="sdt_theme_options[zenphoto_url]" id="zenphoto-url" value="<?php echo esc_attr( $options['zenphoto_url'] ); ?>" size="60" /> |
||
116 | </fieldset> |
||
117 | <?php submit_button(); ?> |
||
118 | </form> |
||
119 | </div> |
||
120 | <?php |
||
121 | } |
||
122 | |||
123 | /* |
||
124 | * Sanitize and validate form input. Accepts an array, return a sanitized array. |
||
125 | */ |
||
126 | function sdt_theme_options_validate($input) { |
||
127 | $output = $defaults = sdt_get_default_theme_options(); |
||
128 | |||
129 | // Zenphoto URL must be in our array |
||
130 | if (isset( $input['zenphoto_url'])) |
||
131 | $output['zenphoto_url'] = $input['zenphoto_url']; |
||
132 | |||
133 | return apply_filters('sdt_theme_options_validate', $output, $input, $defaults); |
||
134 | } |
||
135 | ?> |