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

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.


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