WordPress.org

Make WordPress Core

Ticket #20833: 20833.diff

File 20833.diff, 2.1 KB (added by kobenland, 6 years ago)

Make sure errors from transient get added to the global variable and not unset when checking for an error of a specific setting. Also always return an array, as noted in the documentation.

  • wp-admin/includes/template.php

     
    12241224        // This allows the $sanitize_callback from register_setting() to run, adding
    12251225        // any settings errors you want to show by default.
    12261226        if ( $sanitize )
    1227                 sanitize_option( $setting, get_option($setting));
     1227                sanitize_option( $setting, get_option( $setting ) );
    12281228
    12291229        // If settings were passed back from options.php then use them
    1230         // Ignore transients if $sanitize is true, we don't want the old values anyway
    1231         if ( isset($_GET['settings-updated']) && $_GET['settings-updated'] && get_transient('settings_errors') ) {
    1232                 $settings_errors = get_transient('settings_errors');
    1233                 delete_transient('settings_errors');
    1234         // Otherwise check global in case validation has been run on this pageload
    1235         } elseif ( count( $wp_settings_errors ) ) {
    1236                 $settings_errors = $wp_settings_errors;
    1237         } else {
    1238                 return;
     1230        if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] && get_transient( 'settings_errors' ) ) {
     1231                $wp_settings_errors = array_merge( (array) $wp_settings_errors, get_transient( 'settings_errors' ) );
     1232                delete_transient( 'settings_errors' );
     1233        }
     1234       
     1235        // Check global in case errors have been added on this pageload
     1236        if ( ! count( $wp_settings_errors ) ) {
     1237                return array();
    12391238        }
    12401239
    12411240        // Filter the results to those of a specific setting if one was set
    12421241        if ( $setting ) {
    1243                 foreach ( (array) $settings_errors as $key => $details )
    1244                         if ( $setting != $details['setting'] )
    1245                                 unset( $settings_errors[$key] );
     1242                foreach ( (array) $wp_settings_errors as $key => $details )
     1243                        if ( $setting == $details['setting'] )
     1244                                $setting_errors[] = $wp_settings_errors[$key];
     1245                return $setting_errors;
    12461246        }
    1247         return $settings_errors;
     1247       
     1248        return $wp_settings_errors;
    12481249}
    12491250
    12501251/**
     
    12761277
    12771278        $settings_errors = get_settings_errors( $setting, $sanitize );
    12781279
    1279         if ( ! is_array( $settings_errors ) )
     1280        if ( empty( $settings_errors ) )
    12801281                return;
    12811282
    12821283        $output = '';