WordPress.org

Make WordPress Core

Ticket #11474: settings_validation_errors_interim_hack.txt

File settings_validation_errors_interim_hack.txt, 3.1 KB (added by jeremyclarke, 4 years ago)

Settings Validation Interim Hack Solution

Line 
1/**
2 * Validation callback for all of 'gv_settings' option.
3 *
4 * Referenced in register_setting() for gv_settings, runs whenever update_option('gv_settings') is used.
5 * Must account for every field added to gv_settings via add_settings_field
6 *
7 * @param array $input Full array of data passed into update_option()
8 * @return array Sanitized array of data
9 */
10function gv_settings_validate($input){
11        global $gv;
12       
13        /**
14         * Validate an email address
15         */
16        $email = sanitize_email( $input['email'] );
17        // If its valid add the email to the output array
18        if ( $email AND is_email( $email ) ) :
19                $output['email'] = $email;
20        // Otherwise add a message with a slug and text value to be output on the settings page
21        elseif ( $input['email'] ) :
22                $messages['email_error'] = "Email Error: The 'email address' given, <strong>" . $input['email'] . " </strong> is not a valid email";
23        endif;
24
25
26        /**
27         * Validate a page id
28         */
29        if ($input['special_page_id']) :
30                $page_object = get_page( $input['special_page_id'] );
31                // Make sure a valid page object was returned.
32                if ( is_object( $page_object ) AND !is_wp_error($page_object) )
33                        $output['special_page_id'] = $input['special_page_id'];
34                else
35                        $messages['page_id_error'] = "Page ID Error: The page id given, <strong>" . $input['special_page_id'] . " </strong> is not a page on this site";
36        endif;
37
38        /**
39         * Add any messages to the array
40         */
41        if (is_array($messages))
42                 $output['messages'] = $messages;
43
44        /**
45         * Return the array of new values
46         */
47        return $output;
48}
49
50/**
51 * Show any messages/errors saved to a setting during automated validation
52 *
53 * Needed because validation system has no error reporting.
54 * Uses a ['messages'] array inside the settings array
55 * Format should be ['messages'][$slug][$message_text], $slug is unique id, $message is just text.
56 *
57 * @param string $setting The setting name as used in get_option()
58 */
59function gv_settings_display_errors($setting) {
60        $option = get_option($setting);
61        if (is_array($option['messages'])) :
62                foreach ( (array) $option['messages'] as $slug => $message) :
63                        echo "<div id='gv_messages' class='error fade $slug'><p>$message</p></div>";
64                        unset($option['messages'][$slug]);
65                endforeach;
66                update_option('gv_settings', $option);
67        endif;
68}
69
70/**
71 * Page display function for GV Settings
72 *
73 * Defined during gv_add_settings_pages()
74 */
75function gv_settings_page() {
76        ?>
77        <div>
78                <h2><?php _e('Global Voices Settings'); ?></h2>
79                <p>
80                        These settings are related specifically to Global Voices sites and the gv-plugin plugin.
81                        They cover various specific and general functionality added by gv-plugin.
82                </p>
83
84                <?php gv_settings_display_errors('gv_settings'); ?>
85
86                <form action="options.php" method="post">
87                        <p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
88                        <?php
89                        // Output fundamental security etc. form fields
90                        settings_fields('gv_settings');
91                        // Output any sections defined for page gv_settings
92                        do_settings_sections('gv_settings');
93                        ?>
94                        <p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
95                </form>
96        </div>
97        <?php
98}