passing parameters to do action from shortcode to wp_footer

Driving me nuts.I have a shortcode which works -fine- but for one detail. I won't post the entire thing, but it pul

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

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 asked Oct 17, 2012 at 19:23 jchwebdevjchwebdev 7752 gold badges14 silver badges33 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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;
    }
}

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

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744781782a4593384.html

相关推荐

  • passing parameters to do action from shortcode to wp_footer

    Driving me nuts.I have a shortcode which works -fine- but for one detail. I won't post the entire thing, but it pul

    2天前
    80

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信