Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Driving me nuts.

I have a shortcode which works -fine- but for one detail. I won't post the entire thing, but it pulls the content of a post (works fine) then should echo a portion of it to a new DIV in the footer.

I'm doing it this way because, apparently, you can't pass variables to an anonymous function with add_action.

add_shortcode('tooltip', 'tooltip');
function tooltip( $atts, $content=null) { 
  $output = '...some stuff from another post.';
  //...working fine...

  do_action( 'jch_tooltip_func', 'text to put in footer' );
  // the text arg is never passed to jch_tooltip_func();

  return $output;

}

add_action('wp_footer', 'jch_tooltip_func', 100, 1);

function jch_tooltip_func( $d ) { 
  echo('<p>DIV TEST:' . $d . 'END</p>' );
    return($d);
}

...so 'text to put in footer' should be passed to jch_tooltip_func() and then placed my footer via wp_footer. But the argument never gets passed.

Why oh why?

TIA,

---JC

share|improve this question

2 Answers 2

Use a class, store the value you need in the footer in a member variable.

Sample code, not tested:

add_shortcode( 'tooltip', array ( 'WPSE_69605_Tooltip', 'shortcode_callback' ) );

class WPSE_69605_Tooltip
{
    protected static $var = '';

    public static function shortcode_callback( $atts, $content = '' )
    { 
        self::$var = 'foo';
        add_action( 'wp_footer', array ( __CLASS__, 'footer' ) );
    }

    public static function footer()
    {
        echo self::$var;
    }
}
share|improve this answer

it doesn't work because the first parameter provided to do_action is not function name but tag name. They work like this

add_action( 'tag-name', 'function_name' );

// some other code

do_action( 'tag-name' ); // will call the function 'function_name'

THE SOLUTION

If your text to be added is static like in the example above, remove the add_action('wp_footer', ...) & replace the do_action with

add_action('wp_footer',
 create_function( '', 'echo(\'<p>DIV TEST:\' . \'text to put in footer\' . \'END</p>\' );' ),
 100,
 0);

if your text is dynamic, then you can take one of the 2 approaches

First, use var_export() php function in the code of the above solution

Second, save the variable to some global/static variable(just make sure it's safe) & then where you used add_action above instead of create_function pass the name of the function that will read that variable & output it. A class variable works best for this case but it could be any variable as long as you're sure it's not modified by some other plugin

share|improve this answer
    
Fabulous help. I'm not yet proficient enough with PHP (coming from C) to get all this, but I understand enough to get it working for my needs. One follow up if I may... and this may be old school but... if you use create_function() (or any lambda or anonymous functions) is there proper garbage collection? I want to use this 'tooltip' shortcode several times on a given page and I wonder if overhead or general 'efficiency' is a concern? Thanks Again! ---JC –  jchwebdev Oct 17 '12 at 22:33
    
anonymous functions are only available in php 5.3 onwards. I don't know about garbage collection –  Mridul Aggarwal Oct 18 '12 at 7:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.