WordPress.org

Make WordPress Core

Opened 19 months ago

Last modified 10 months ago

#43695 new defect (bug)

$depth and $args are switched when using custom callback in wp_list_comments()

Reported by: vincurekf Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.5
Component: Comments Keywords:
Focuses: docs, template Cc:
PR Number:

Description

For the custom function I am using the [latest function from walker](https://github.com/WordPress/WordPress/blob/master/wp-includes/class-walker-comment.php#L343) as a base.

When you use custom callback in comments.php template:

<?php
// comments.php
$args = [
    'type' => 'comment',
    'format' => 'html5',
    'style' => 'ol',
    'short_ping' => true,
];
// Use our custom callback if it's available
if( function_exists( 'custom_render_comment' ) ){
    $args['format'] = 'custom';
    $args['callback'] = 'custom_render_comment';
}
wp_list_comments( $args );

The arguments that get passed to custom_render_comment function are switched:

<?php
// custom_render_comment.php
function custom_render_comment( $comment, $depth, $args ) {
/*
var_dump($depth):
array(15) { ["walker"]=> NULL ["max_depth"]=> string(1) "5" ["style"]=> string(2) "ol" ["callback"]=> string(21) "faeiv2_render_comment" ["end-callback"]=> NULL ["type"]=> string(7) "comment" ["page"]=> int(0) ["per_page"]=> int(0) ["avatar_size"]=> int(32) ["reverse_top_level"]=> bool(false) ["reverse_children"]=> string(0) "" ["format"]=> string(6) "faeiv2" ["short_ping"]=> bool(true) ["echo"]=> bool(true) ["has_children"]=> bool(true) }
}
var_dump($args):
int(1)
*/
}

The result is this error:

Warning: array_merge(): Argument #1 is not an array in [...]/wp-content/themes/mytheme/inc/render-comment.php on line 56

I haven't found any mention of switching arguments in the WordPressCodex.

The fix is easy, just switch those variables, but I think it should be addressed somewhere in the documentation.

Change History (2)

#1 @soulseekah
19 months ago

Welcome to Trac!

The fix is easy, just switch those variables

For back-compatibility reasons this would be a bad idea.

Docs? Probably a good idea.

Although https://codex.wordpress.org/Function_Reference/wp_list_comments lists an example (mytheme_comment).

Either way: https://codex.wordpress.org/Codex:Contributing

Note: See TracTickets for help on using tickets.