/**
* Define a new function that uses $args and wp_parse_args()
*/
function explain_parse_args( $args ) {
$defaults = array (
'text' => 'wp_parse_args() merges $args into $defaults',
'before' => "",
'after' => "
\n",
'echo' => TRUE
);
// Parse incomming $args into an array and merge it with $defaults
$args = wp_parse_args( $args, $defaults );
// OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
extract( $args, EXTR_SKIP );
$output = $before . $text . $after;
if (!$echo)
return $output;
echo $output;
}
/**
* Run our new function using the defaults (no $args)
* This would print out:
* wp_parse_args() merges $args into $defaults
*/
explain_parse_args();
/**
* Run the function with some options overriden with an array
* This would echo the output with the modified text and before arguments:
* A better explanation
*/
explain_parse_args( array (
'text' => "A better explanation",
'before' => ""
) );
/**
* We can also pass in URL-style string-query and it will be converted
* This would set $args['echo'] to 1 and $args['text'] to 0
*/
explain_parse_args( 'echo=1&text=0' );