Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge multiple WP_Error objects together.
<?php
function wpcom_wp_error_merge() {
$wp_error_merged = new WP_Error();
$wp_errors = func_get_args();
foreach ( $wp_errors as $wp_error ) {
if ( ! is_wp_error( $wp_error ) )
continue;
foreach ( $wp_error as $key => $errors ) {
foreach ( $errors as $error ) {
$wp_error_merged->add( $key, $error );
}
if ( isset( $wp_error->error_data[ $key ] ) ) {
$wp_error_merged->add_data( $wp_error->error_data[ $key ], $key );
}
}
}
return $wp_error_merged;
}
@wpscholar

This comment has been minimized.

Copy link

commented Feb 14, 2014

Thanks for sharing this code! I was needing something like this, but your code doesn't take into account preserving the error keys since your iterator is using the numeric index as the key. I created a more robust solution based on your original code: https://gist.github.com/woodent/9004667.

@stephanethomas

This comment has been minimized.

Copy link

commented Jan 9, 2015

@wpscholar, your link doesn't lead anywhere. I guess it should be that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.