admin管理员组

文章数量:1391977

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;
}

本文标签: actionsHow to manage arrays from custom functions stored in functionsphp