Passing values into WordPress shortcode

I’m trying to write a WordPress shortcode in php. I’m having trouble passing values from the shortcode to the rendered content though.

function tweet($atts) {

extract( shortcode_atts( array(
                'id'            => '',
                'width'         => '520',
                'height'        => '600',
                'theme'         => 'light',
                'link_color'    => '#333333',
                'border_color'  => '#e8e8e8',
                'header'        => 'true',
                'footer'        => 'true',
                'border'        => 'true',
                'scrollbar'     => 'true',
                'transparent'   => 'false',
                'tweet_limit'   => '10',
        ), $atts ) );



return '<a class="twitter-timeline"  
data-widget-id="<?php echo $this->id; ?>"
width="<?php echo $this->width; ?>"
height="<?php echo $this->height; ?>"
data-theme="<?php echo $this->theme; ?>" 
data-link-color="<?php echo $this->linkColor; ?>"
data-border-color=<?php echo $this->borderColor; ?>
data-chrome="<?php echo $this->chrome; ?>"
data-tweet-limit="<?php echo $this->tweetLimit; ?>"
lang="<?php echo $this->lang; ?>"></a>';
}
add_shortcode('twitter', 'tweet');

If I hard code values into fields like, data-widget-id, then it will work. However, if I just write [twitter id="707980844590342144"] the value doesn’t get passed in.

Read More

It seems like something along these lines might be the solution, but I haven’t been able to get it working so far:

        $this->id           = $id;
        $this->width        = $width;
        $this->height       = $height;
        $this->theme        = $theme;
        $this->linkColor    = $link_color;
        $this->borderColor  = $border_color;
        $this->tweetLimit   = $tweet_limit;
        $this->chrome       = "";
        $this->chrome       .= ( $header == 'false')        ? 'noheader ' : '';
        $this->chrome       .= ( $footer == 'false')        ? 'nofooter ' : '';
        $this->chrome       .= ( $borders == 'false')       ? 'noborders ' : '';
        $this->chrome       .= ( $scrollbar == 'false')     ? 'noscrollbar ' : '';
        $this->chrome       .= ( $transparent == 'true')    ? 'transparent ' : '';

Related posts

2 comments

  1. If you want to insert variables inside string use double quotes and variable name inside curly brackets (not necessary if you do not using arrays or objects):

    return "<a class="twitter-timeline"  
      data-widget-id="{$id}"
      width="{$width}"
      height="{$height}"
      data-theme="{$theme}" 
      data-link-color="{$linkColor}"
      data-border-color="{$borderColor}"
      data-chrome="{$chrome}"
      data-tweet-limit="{$tweetLimit}"
      lang="{$lang}"></a>";
    

    But I prefer sprintf functions for it:

    return sprintf(
      '<a class="twitter-timeline" data-widget-id="%s" width="%s" height="%s" data-theme="%s" data-link-color="%s" data-border-color="%s" data-chrome="%s" data-tweet-limit="%s" lang="%s"></a>',
      $id,
      $width,
      $height,
      $theme,
      $linkColor,
      $borderColor,
      $chrome,
      $tweetLimit,
      $lang
    );
    
  2. Try this into return statement…

    return '<a class="twitter-timeline"  
        data-widget-id="<?php echo $id; ?>"
        width="<?php echo $width; ?>"
        height="<?php echo $height; ?>"
        data-theme="<?php echo $theme; ?>" 
        data-link-color="<?php echo $linkColor; ?>"
        data-border-color=<?php echo $borderColor; ?>
        data-chrome="<?php echo $chrome; ?>"
        data-tweet-limit="<?php echo $tweetLimit; ?>"
        lang="<?php echo $lang; ?>"></a>';
    

Comments are closed.