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 badges1 Answer
Reset to default 2A 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
版权声明:本文标题:actions - How to manage arrays from custom functions stored in functions.php? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744703475a2620698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论