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,
);
4
  • 1
    Do you want to return value of the function in the array or the ACTUAL function in the array? Commented Jul 24, 2013 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 Commented Jul 24, 2013 at 13:14
  • anonymous function ?? Commented Jul 24, 2013 at 13:23
  • You could indeed use Anonymous Functions, check out Vladimir Hraban's answer for that. Commented Jul 24, 2013 at 13:25

4 Answers 4

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(),
);
Sign up to request clarification or add additional context in comments.

1 Comment

I think that he want to use this function somewhere else and trying to store it on $arr :)
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();

Comments

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

Comments

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),
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.