Skip to content

Instantly share code, notes, and snippets.

@jdevalk
Created April 11, 2013 08:53
Show Gist options
  • Save jdevalk/5361844 to your computer and use it in GitHub Desktop.
Save jdevalk/5361844 to your computer and use it in GitHub Desktop.
<?php
// If you have a function with a default argument:
function bla ( $echo = true ) {
if ( !$echo )
return 'bla';
else
echo 'bla';
}
// And you then hook this function:
add_action( 'wp_head', 'bla' );
// $echo remains unset and thus !$echo == true and the function returns instead of echos...
// Solutiuon, of course, is to change
if ( !$echo )
return 'bla';
// Into
if ( $echo === false )
return 'bla';
// But it takes some debugging to get there :)
@ramondelafuente
Copy link

Wordpress actions have set parameters; and wp_head has 0;
http://codex.wordpress.org/Function_Reference/wp_head

This means, if you add an action and tell wordpress that your function does have a parameter, it will be an empty string by default (when you call add_action with 2 params, you're saying your function has 1 param, which is the default value).
http://codex.wordpress.org/Function_Reference/add_action

add_action( 'wp_head', 'bla'); *
is actually:
*add_action( 'wp_head', 'bla', 10, 1);

if you try:
add_action( 'wp_head', 'bla', 10, 0 );

Now wordpress won't call bla(''), but bla() -- and you'll get your $echo == true.
Problem is, it wil always be true, so that makes the parameter useless...

@ramondelafuente
Copy link

By default, wp_head() does nothing but call do_action('wp_head')

It's probably bad form, but if -in your template- you replace:
wp_head();

with:
do_action('wp_head', true);

Your parameter will be passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment