To pass variables into functions, I do the following (as other people I’m sure):
function addNums($num1, $num2)
{
$num1 + $num2;
}
addNums(2, 2);
My question is how would I structure a function to act like WordPress:
wp_list_categories('title_li=');
Essentially I am looking for a way to create a key/value pair in my functions.
Any advice is appreciated.
You can use parse_str to parse the string for arguments. The tricky thing is that you may not want to just allow any and all parameters to get passed in. So here’s an example of only allowing certain parameters to be used when they’re passed in.
In the following example, only
foo,barandvalidwould be allowed.bazwill be dropped because it is not listed in$valid_arguments. So for this call:Results:
Additionally, you can browse the WordPress Source code here, and of course by downloading it from wordpress.org. Looks like they do something very similar.