actions - How to manage arrays from custom functions stored in functions.php?

In functions.php I have made the following code:add_action( 'listofnames', 'SomeNames' );function So

In functions.php I have made the following code:

add_action( 'listofnames', 'SomeNames' );

function SomeNames(){

$names=array( "john","edgar","miles");
return $names;

}

It should return array I wish to manipulate with, but when in index.php I try, it goes wrong. Obviously it does not work this way:

do_action('listofnames');
foreach (do_action('listofnames') as $n){

echo $n;
}

Can anybody help me to manage this?

In functions.php I have made the following code:

add_action( 'listofnames', 'SomeNames' );

function SomeNames(){

$names=array( "john","edgar","miles");
return $names;

}

It should return array I wish to manipulate with, but when in index.php I try, it goes wrong. Obviously it does not work this way:

do_action('listofnames');
foreach (do_action('listofnames') as $n){

echo $n;
}

Can anybody help me to manage this?

Share Improve this question asked Mar 2, 2020 at 1:14 Bunny DavisBunny Davis 1133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

A callback for an action doesn't return anything, because that return value is never passed through by WordPress.

Use a filter instead:

add_filter( 'listofnames', 'SomeNames' );

function SomeNames() {

    $names=array( "john","edgar","miles");
    return $names;
}

And in your template you call it like this:

$names = apply_filters( 'listofnames', [] );

foreach ( $names a $name ) {
    echo $name;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744703475a4588912.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信