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.
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 ' : '';
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):
But I prefer
sprintf
functions for it:Try this into return statement…