Created
June 26, 2013 20:49
-
-
Save mjangda/5871525 to your computer and use it in GitHub Desktop.
Merge multiple WP_Error objects together.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
For anyone hitting this page, as of WordPress 5.6 WP_Error allows merging objects together. See https://core.trac.wordpress.org/ticket/38777
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@wpscholar, your link doesn't lead anywhere. I guess it should be that one.