Carlos Faria
Carlos Faria

Reputation: 301

Using a function as Callback of another one declared with namespaces

I'm using WordPress Sage Starter theme (https://roots.io/sage/). It uses namespaces for function declarations.

I have a function (my_function_comments) in my lib/extras.php file to modify the comments markup. This file has this namespace: namespace Roots\Sage\Extras; Now I need to use that function as a callback in another file templates/comments.php as this:

<?php wp_list_comments(array('style' => 'ol', 
                          'short_ping' => true,
                          'avatar_size' => 60,
                          'type' => 'comment',
                          'callback' => 'my_function_comments',
                          )); ?>

Of course, my_function_comments is not on this file so I've coded the call to wp_list_comments like this:

<?php use Roots\Sage\Extras; ?>
<?php wp_list_comments(array('style' => 'ol', 
                          'short_ping' => true,
                          'avatar_size' => 60,
                          'type' => 'comment',
                          'callback' => 'Extras\my_function_comments',
                          )); ?>

Ok, the callback function as this is obviouslly wrong, but I don't know how to write it to call it properly.

Maybe someone can help me to figure out this.

Thank you!

PS. Namespaces related documentation is found here (Namespaces section): https://roots.io/upping-php-requirements-in-your-wordpress-themes-and-plugins/. Maybe that will help a bit to answer my question.

Upvotes: 2

Views: 1350

Answers (1)

Carlos Faria
Carlos Faria

Reputation: 301

As @Wesee said in the comments, the way to use the function as a callback is putting the whole path to the function. So then I removed this line:

<?php use Roots\Sage\Extras; ?>

And use this to call wp_list_comments:

<?php wp_list_comments(array('style' => 'ol', 
                      'short_ping' => true,
                      'avatar_size' => 60,
                      'type' => 'comment',
                      'callback' => 'Roots\Sage\Extras\my_function_comments',
                      )); ?>

Thank you guys!

Upvotes: 2

Related Questions