wp_parse_args recursive of multidimensional arrays

wp_parse_args is a wonderful function for combining two arrays, objects, or strings in WordPress. But it lacks the possibility to deeply merge multidimensional arrays.

Usually, I use this function just to merge arrays. I don’t use it to merge objects or strings. Most of the time I have large arrays which don’t need to be combined with more than one level. And that’s perfectly fine, to use the default WP function. However, sometimes I need to go deeper and combine the arrays down to the highest level. How do we do that? Simply by creating a new function of course.

Note: This new function will combine only arrays. Don’t pass objects or strings like you’d do in wp_parse_args.

if ( ! function_exists( 'awps_recursive_parse_args' ) ) {
    function awps_recursive_parse_args( $args, $defaults ) {
        $new_args = (array) $defaults;
        foreach ( $args as $key => $value ) {
            if ( is_array( $value ) && isset( $new_args[ $key ] ) ) {
                $new_args[ $key ] = awps_recursive_parse_args( $value, $new_args[ $key ] );
            else {
                $new_args[ $key ] = $value;
        return $new_args;

If you are looking for a test case, see my answer on wp.org: https://developer.wordpress.org/reference/functions/wp_parse_args/#comment-2556

Happy WordPressing :))

Member since January 2, 2019

Fullstack Web Developer with more than 12 years of experience in web development. Adept in all stages of advanced web development. Knowledgeable in the user interface, backend, testing, and debugging processes. Bringing forth expertise in design, installation, testing, and maintenance of web systems. Working exclusively and professionally with WordPress since 2010.


    Your email address will not be published. Required fields are marked *