1

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

3

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;
    }
}
| improve this answer | |
0

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

| 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
0

I know this is old but here is my answer and it can help anybody who's having similar challenge as mine. Based on @fuxia code, her code works perfectly but will only display one output. Assuming you have multiple instances of the shortcode on a page, Only one action hook will be added to footer.

If you want to add multiple action based on the number of times your shortcode is used on a page use the code below;

add_shortcode( 'izzycart_popup', array ( 'MFP_PopUP_Shortcode', 'load_shortcode' ) );

class MFP_PopUP_Shortcode {

    protected static $var = array();

    public static function load_shortcode( $atts, $content = '' ) {
        /**
        * Write all your shortcode stuff here.
        * Make sure you use either unique_id() or mt_rand() to generate ID in your
        * shortcode for later use
        **/

        /**
        * Depending on what you're using this for, send parameters from shortcode to
        * new function. I used this for a iPad frame Video, So i parsed the following
        * @params $video_url  -  Video URL from the shortcode_atts()
        * @params $video_poster  -  Video poster id from the shortcode_atts()
        * @params $el_id  -  element ID generated from either unique_id() or mt_rand()
        * @params array $OtherContent  -  Array of other values you want to parse to the function
        **/
        self::$var[] = self::load_the_popupcontent($video_url, $video_poster, $el_id, $OtherContent); 
        add_action( 'wp_footer', array ( __CLASS__, 'send_to_footer' ) );
    }

    public static function send_to_footer() {
        foreach (self::$var as $theElementToFooter) {
             echo $theElementToFooter;
        }
    }
    public static function load_the_popupcontent( $url='', $poster='', $video_id = '', $args = array() ) {
        // Write all your code to be added to footer here and use all the variables where needed.
    }
} 

NOTE: If you want to add just one element to footer regardless of how many times the shortcode is used, @fuxia's answer is what you'd go for.

Tested and Works!
Happy Coding!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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