0

Is it possible to include this function

function Get_All_Wordpress_Menus(){
    return get_terms( 'nav_menu', array( 'hide_empty' => true ) ); 
}

into this array

array(
    'options' => ADD_FUNCTION_HERE,
);
Dean Elliott
  • 727
  • 2
  • 13
  • 26
  • 1
    Do you want to return value of the function in the array or the ACTUAL function in the array? – MisterBla Jul 24 '13 at 13:11
  • I want to return to value of the function (It's a select box that shows a list of menus that exist within a Wordpress based site, so I need to display that list with each menu as an select option). Sorry, PHP isn't really my forte – Dean Elliott Jul 24 '13 at 13:14
  • anonymous function ?? – Arun Killu Jul 24 '13 at 13:23
  • You could indeed use Anonymous Functions, check out Vladimir Hraban's answer for that. – MisterBla Jul 24 '13 at 13:25

4 Answers4

0

You need this ?

function Get_All_Wordpress_Menus(){
    return get_terms( 'nav_menu', array( 'hide_empty' => true ) ); 
}

$arr = array(
    'options' => Get_All_Wordpress_Menus(),
);
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • I think that he want to use this function somewhere else and trying to store it on `$arr` :) – Red Jul 24 '13 at 13:12
0

If you want to store the function in an array do this:
Example

function foo($text = "Bar")
{
    echo $text;
}

// Pass the function to the array. Do not use () here.
$array = array(
    'func' => "foo" // Reference to function
);

// And call it.
$array['func'](); // Outputs: "Bar"
$array['func']("Foo Bar"); // Outputs: "Foo Bar"

If you need to pass the return value, it's very simple (assuming previous example):

$array['value'] = foo();
MisterBla
  • 2,355
  • 1
  • 18
  • 29
0

If you need to store the function itself, use anonymous functions

$arr = array(
    'options' => function()
                {
                    return get_terms( 'nav_menu', array( 'hide_empty' => true ) );  
                }
);

You can then call it like

$func = $arr['options'];
$func();

http://php.net/manual/en/functions.anonymous.php

Note that before PHP 5.3 it was not possible. Although there is a workaround that is described in Closure objects within arrays before PHP 5.3

Community
  • 1
  • 1
Vladimir Hraban
  • 3,543
  • 4
  • 26
  • 46
0
  function Get_All_Wordpress_Menus($call){


$call = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); 
return $call;
 }


$array = array(
'options' => $call,
);

OR

 $array = array(
'options' => Get_All_Wordpress_Menus($call),
);
Steven Sze
  • 19
  • 2
  • 8