- This topic is empty.
-
AuthorPosts
-
February 26, 2011 at 11:26 pm #31796jacorreParticipant
Playing around with a plugin for WordPress. I’m trying to figure out how to pass multiple values into a function and then use those values within the function.
For example:
So if someone uses that template tag and passes in the values of 1, 2, and 3, I want to grab all those values they pass in and do something with them in the function I’m writing.
How would I go about doing that? I believe I need a loop to to store each value into an array right?
February 27, 2011 at 8:32 am #57919BobMemberLet’s say you want to add up three numbers, which are the variables $var1, $var2 and $var3.
The following code would be the code for your function:
function myfunction($var1, $var2, $var3) {
$total = $var1 + $var2 + $var3;
echo $total;
}
?>That adds them up and will output the total, if the function is used.
To use the function, you can use the code you posted above, and it will output 6. Hope this helps and is what you mean.
February 27, 2011 at 11:16 am #57921jacorreParticipantThanks Bob. I think that’s fine if we know only three values are being passed in. If I don’t know how many values will be passed in, how should I approach it?
For example, if the values being passed in are category ids like 1,2,3,4, I want to take those values and plug them into a function that uses exclude=1,2,3,4.
February 27, 2011 at 1:19 pm #57890TT_MarkMemberThe you want to pass an array
callSomeFunction( array('1', '2', '3') ) ;
function callSomeFunction( $parameters=array() )
{
// Check array is not empty
if ( !is_empty( $parameters ) )
{
// Loop through array values
foreach ( $parameters AS $variable )
{
// Do Something
}
}
}
?>February 27, 2011 at 4:27 pm #57891jacorreParticipantThanks TT_Mark. I think it’s a bit more complicated because of what I need to pass it into.
So if someone used
, then I wanted the values of 1,2,3 to be passed into
get_categories('exclude=1,2,3')
February 27, 2011 at 10:43 pm #57871richtestaniMemberWhile I’m not sure what you want to do, there a re a few ways to handle an unknown number of arguments.
You could use the func_get_args() function which is used like:
function my_function() {
$args = func_get_args();
foreach($args as $val) {
echo $val;
}
}
my_function(1, 2, 3, 4); --> 1234
Another way is restricting to associative array, so you have named values.
function my_function($array) {
extract($array);
if(isset($exclude)) {
get_categories('exclude='.$exclude);
}
}
$array = array('exclude'=>'1, 2, 3');
my_function($array);
I’m not much of a WordPress person but it seems many of the arguments are passed name/value pairs as string, so you’d need to build some of those strings.
I have no idea if I helped here.
February 28, 2011 at 4:00 am #57874TT_MarkMemberI have never heard of fun_get_args() before! Cheers for that @richtestani
February 28, 2011 at 8:42 am #57716Chris CoyierKeymasterSuper helpful thread guys, love it.
February 28, 2011 at 11:54 am #57728jacorreParticipantThanks everyone. I think I may have been overcomplicating it. I can actually pass in ‘exclude=1,2,3’ as the argument and it works! So for example:
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.