Content from shortcode within post NOT showing in widget

January 2013 WordPress Theme
GK User
Mon Mar 03, 2014 5:20 am
Hi All,

I have placed the shortcode, for a news ticker (and a RSS fed) into a post. The content from the feed shows up in the post with no problem. However, the content from the shortcode that I placed in the post will not show up in the GK News Slideshow and Tabs widgets. All I see in the widget is the post title and the actual shortcode.

How do I get the content from the shortcode that I placed in the post to show up in the GK News Slideshow and Tabs widgets?
User avatar
Fresh Boarder

GK User
Mon Mar 03, 2014 9:30 am
Hi,

Slider and tabs content are filtered from the shortocodes. You can edit News/gavern/widget.slideshow.php file and add this line
Code: Select all
$art_text = apply_filters('the_content', $art_text);
in generate_art_text function, before this line:
Code: Select all
$art_text = $this->cut_text('article_text', $art_text);


and you should remove this line:
Code: Select all
$text = strip_tags($text);
from cut_text function (at the end of file).
User avatar
Moderator

GK User
Mon Mar 03, 2014 10:07 pm
Hi,

Thanks for the reply. I was excited to see that it could be done, but after I completed the task, it seems to not have worked. I followed your instructions, but I can still see the shortcode from the posts in the widget.

I have attached the "widgets.slideshow.php" file that I edited. Please take a look at the file to ensure that I edited it correctly.

NOTE: dont forget to add the php extension to the file I uploaded. Your software would not allow me to upload a php file.

Attaching the file didn't work..... here's the code


<?php

/**
*
* GK News Show Pro Widget class
*
**/

// EDITE LINES 422-424 AND 483 //


class GK_News_Slideshow_Widget extends WP_Widget {
// variable used to store the object configuration
private $wdgt_config;
// variable uset to store the object query results
private $wdgt_results;

/**
*
* Constructor
*
* @return void
*
**/
function GK_News_Slideshow_Widget() {
$this->WP_Widget(
'widget_gk_news_slideshow',
__('GK News Slideshow', GKTPLNAME),
array(
'classname' => 'widget_gk_news_slideshow',
'description' => __( 'Use this widget to show recent items as slideshow', GKTPLNAME)
),
array(
'width' => 320,
'height' => 350
)
);

$this->alt_option_name = 'widget_gk_news_slideshow';
//
add_action('edit_post', array(&$this, 'refresh_cache'));
add_action('delete_post', array(&$this, 'refresh_cache'));
add_action('trashed_post', array(&$this, 'refresh_cache'));
add_action('save_post', array(&$this, 'refresh_cache'));
//
add_action('wp_enqueue_scripts', array('GK_News_Slideshow_Widget', 'add_scripts'));
}

static function add_scripts() {
wp_register_script( 'gk-news-slideshow', get_template_directory_uri() . '/js/widgets/slideshow.js', array('jquery'));
wp_enqueue_script('gk-news-slideshow');
}

/**
*
* Outputs the HTML code of this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void
*
**/
function widget($args, $instance) {
$cache = get_transient(md5($this->id));
// the part with the title and widget wrappers cannot be cached!
// in order to avoid problems with the calculating columns
//
extract($args, EXTR_SKIP);

$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);

$ops = array('data_source_type', 'data_source', 'orderby', 'order', 'offset', 'article_amount', 'article_width', 'article_title_len', 'article_title_len_type', 'article_text_len', 'article_text_len_type', 'article_image_w', 'article_image_h', 'cache_time');

foreach($ops as $option) {
$config[$option] = empty($instance[$option]) ? null : $instance[$option];
}

echo $before_widget;

if($title != '') {
echo $before_title;
echo $title;
echo $after_title;
}

if($cache) {
echo $cache;
echo $after_widget;
return;
}
// start cache buffering
ob_start();
// get the posts data
// let's save the global $post variable
global $post;
$tmp_post = $post;
//
// other options for the query
//
// total amount of the posts
$amount_of_posts = $config['article_amount'];
// resutls array
$results = array();
// data source
if($config['data_source_type'] == 'latest') {
$results = get_posts(array(
'posts_per_page' => $amount_of_posts,
'offset' => $config['offset'],
'orderby' => $config['orderby'],
'order' => $config['order']
));
} else if($config['data_source_type'] == 'category') {
$results = get_posts(array(
'category_name' => $config['data_source'],
'posts_per_page' => $amount_of_posts,
'offset' => $config['offset'],
'orderby' => $config['orderby'],
'order' => $config['order']
));
} else if($config['data_source_type'] == 'post') {
$post_slugs = explode(',', $config['data_source']);
foreach($post_slugs as $slug) {
array_push($results, get_posts(array('name' => $slug)));
}
} else if($config['data_source_type'] == 'custom') {
$post_type = explode(',', $config['data_source']);
array_push($results, get_posts(array('post_type' => $post_type, 'numberposts' => $amount_of_posts)));
}
// restore the global $post variable
$post = $tmp_post;
// generate the widget wrapper
echo '<div class="gk-news-slideshow">';
// generate the articles
$amount_of_articles = $config['article_amount'];
// iterate
$this->wdgt_config = $config;
$this->wdgt_results = $results;
// wrap articles
echo '<div class="gk-ns-images">';
echo '<div class="gk-ns-arts">';
echo '<div class="gk-ns-arts-scroll">';
//
$i = 0;
//
for($r = 0; $r < $amount_of_articles; $r++) {
if(isset($results[$i]) || isset($results[0][$i])) {
$art_title = $this->generate_art_title($i);
$art_text = $this->generate_art_text($i);
$art_image = $this->generate_art_image($i);
//
$art_output = $art_image . $art_title . $art_text;
// the final output
echo '<div class="gk-ns-art" style="width: '.$config['article_width'].'px;">' . $art_output . '</div>';
}
//
$i++;
}
//
echo '</div>';
// closing the wrappers
echo '</div>';
echo '</div>';
//
echo '<div class="gk-ns-prev">&laquo;</div>';
echo '<div class="gk-ns-next">&raquo;</div>';
//
echo '</div>';
// save the cache results
$cache_output = ob_get_flush();
$cache_time = ($this->wdgt_config['cache_time'] == '' || !is_numeric($this->wdgt_config['cache_time'])) ? 60 : (int) $this->wdgt_config['cache_time'];
set_transient(md5($this->id) , $cache_output, $cache_time * 60);
//
echo $after_widget;
}

/**
*
* Used in the back-end to update the module options
*
* @param array new instance of the widget settings
* @param array old instance of the widget settings
* @return updated instance of the widget settings
*
**/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$ops = array('data_source_type', 'data_source', 'orderby', 'order', 'offset', 'article_amount', 'article_width', 'article_title_len', 'article_title_len_type', 'article_text_len', 'article_text_len_type', 'article_image_w', 'article_image_h', 'cache_time');

foreach($ops as $option) {
$instance[$option] = strip_tags( $new_instance[$option] );
}

$this->refresh_cache();

$alloptions = wp_cache_get('alloptions', 'options');
if(isset($alloptions['widget_gk_news_slideshow'])) {
delete_option( 'widget_gk_news_slideshow' );
}

return $instance;
}

/**
*
* Refreshes the widget cache data
*
* @return void
*
**/

function refresh_cache() {
delete_transient(md5($this->id));
}

/**
*
* Outputs the HTML code of the widget in the back-end
*
* @param array instance of the widget settings
* @return void - HTML output
*
**/
function form($instance) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';

// data source
$data_source_type = isset($instance['data_source_type']) ? esc_attr($instance['data_source_type']) : 'latest';
$data_source = isset($instance['data_source']) ? esc_attr($instance['data_source']) : '';
$orderby = isset($instance['orderby']) ? esc_attr($instance['orderby']) : 'ID';
$order = isset($instance['order']) ? esc_attr($instance['order']) : 'DESC';
$offset = isset($instance['offset']) ? esc_attr($instance['offset']) : '0';

// articles amount
$article_amount = isset($instance['article_amount']) ? esc_attr($instance['article_amount']) : '1';
$article_width = isset($instance['article_width']) ? esc_attr($instance['article_width']) : '262';

// article title format
$article_title_len = isset($instance['article_title_len']) ? esc_attr($instance['article_title_len']) : '10';
$article_title_len_type = isset($instance['article_title_len_type']) ? esc_attr($instance['article_title_len_type']) : 'words';

// article text format
$article_text_len = isset($instance['article_text_len']) ? esc_attr($instance['article_text_len']) : '20';
$article_text_len_type = isset($instance['article_text_len_type']) ? esc_attr($instance['article_text_len_type']) : 'words';

// article text format
$article_image_w = isset($instance['article_image_w']) ? esc_attr($instance['article_image_w']) : '160';
$article_image_h = isset($instance['article_image_h']) ? esc_attr($instance['article_image_h']) : '120';

// cache time
$cache_time = isset($instance['cache_time']) ? esc_attr($instance['cache_time']) : '60';

?>
<div class="gk-nsp-col gk-slideshow">
<h3><?php _e('Basic settings', GKTPLNAME); ?></h3>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', GKTPLNAME ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>

<h3><?php _e('Data source settings', GKTPLNAME); ?></h3>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'data_source_type' ) ); ?>"><?php _e( 'Data source:', GKTPLNAME ); ?></label>

<select id="<?php echo esc_attr( $this->get_field_id('data_source_type')); ?>" name="<?php echo esc_attr( $this->get_field_name('data_source_type')); ?>">
<option value="latest"<?php echo (esc_attr($data_source_type) == 'latest') ? ' selected="selected"' : ''; ?>>
<?php _e('Latest posts', GKTPLNAME); ?>
</option>
<option value="category"<?php echo (esc_attr($data_source_type) == 'category') ? ' selected="selected"' : ''; ?>>
<?php _e('Categories slugs', GKTPLNAME); ?>
</option>
<option value="post"<?php echo (esc_attr($data_source_type) == 'post') ? ' selected="selected"' : ''; ?>>
<?php _e('Posts slugs', GKTPLNAME); ?>
</option>
<option value="custom"<?php echo (esc_attr($data_source_type) == 'custom') ? ' selected="selected"' : ''; ?>>
<?php _e('Custom post types', GKTPLNAME); ?>
</option>
</select>

<textarea id="<?php echo esc_attr( $this->get_field_id('data_source')); ?>" name="<?php echo esc_attr( $this->get_field_name('data_source')); ?>"><?php echo esc_attr($data_source); ?></textarea>
</p>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php _e( 'Order by:', GKTPLNAME ); ?></label>

<select id="<?php echo esc_attr( $this->get_field_id('orderby')); ?>" name="<?php echo esc_attr( $this->get_field_name('orderby')); ?>">
<option value="ID"<?php echo (esc_attr($orderby) == 'ID') ? ' selected="selected"' : ''; ?>>
<?php _e('ID', GKTPLNAME); ?>
</option>

<option value="date"<?php echo (esc_attr($orderby) == 'date') ? ' selected="selected"' : ''; ?>>
<?php _e('Date', GKTPLNAME); ?>
</option>

<option value="title"<?php echo (esc_attr($orderby) == 'title') ? ' selected="selected"' : ''; ?>>
<?php _e('Title', GKTPLNAME); ?>
</option>

<option value="modified"<?php echo (esc_attr($orderby) == 'modified') ? ' selected="selected"' : ''; ?>>
<?php _e('Modified', GKTPLNAME); ?>
</option>

<option value="rand"<?php echo (esc_attr($orderby) == 'rand') ? ' selected="selected"' : ''; ?>>
<?php _e('Random', GKTPLNAME); ?>
</option>
</select>

<select id="<?php echo esc_attr( $this->get_field_id('order')); ?>" name="<?php echo esc_attr( $this->get_field_name('order')); ?>">
<option value="ASC"<?php echo (esc_attr($order) == 'ASC') ? ' selected="selected"' : ''; ?>>
<?php _e('ASC', GKTPLNAME); ?>
</option>
<option value="DESC"<?php echo (esc_attr($order) == 'DESC') ? ' selected="selected"' : ''; ?>>
<?php _e('DESC', GKTPLNAME); ?>
</option>
</select>
</p>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>"><?php _e( 'Offset:', GKTPLNAME ); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'offset' ) ); ?>" type="text" value="<?php echo esc_attr( $offset ); ?>" class="short" />
</p>

<p>
<h3><?php _e('Articles configuration', GKTPLNAME); ?></h3>
<label for="<?php echo esc_attr( $this->get_field_id( 'article_amount' ) ); ?>"><?php _e( 'Amount:', GKTPLNAME ); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'article_amount' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'article_amount' ) ); ?>" type="text" value="<?php echo esc_attr( $article_amount ); ?>" class="short" />
</p>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'article_width' ) ); ?>"><?php _e( 'Article block width (px):', GKTPLNAME ); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'article_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'article_width' ) ); ?>" type="text" value="<?php echo esc_attr( $article_width ); ?>" class="short" />
</p>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'article_title_len' ) ); ?>"><?php _e( 'Title length:', GKTPLNAME ); ?></label>

<input id="<?php echo esc_attr( $this->get_field_id( 'article_title_len' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'article_title_len' ) ); ?>" type="text" value="<?php echo esc_attr( $article_title_len ); ?>" class="short" />

<select id="<?php echo esc_attr( $this->get_field_id('article_title_len_type')); ?>" name="<?php echo esc_attr( $this->get_field_name('article_title_len_type')); ?>">
<option value="chars"<?php echo (esc_attr($article_title_len_type) == 'chars') ? ' selected="selected"' : ''; ?>>
<?php _e('Chars', GKTPLNAME); ?>
</option>
<option value="words"<?php echo (esc_attr($article_title_len_type) == 'words') ? ' selected="selected"' : ''; ?>>
<?php _e('Words', GKTPLNAME); ?>
</option>
</select>
</p>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'article_text_len' ) ); ?>"><?php _e( 'Text length:', GKTPLNAME ); ?></label>

<input id="<?php echo esc_attr( $this->get_field_id( 'article_text_len' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'article_text_len' ) ); ?>" type="text" value="<?php echo esc_attr( $article_text_len ); ?>" class="short" />

<select id="<?php echo esc_attr( $this->get_field_id('article_text_len_type')); ?>" name="<?php echo esc_attr( $this->get_field_name('article_text_len_type')); ?>">
<option value="chars"<?php echo (esc_attr($article_text_len_type) == 'chars') ? ' selected="selected"' : ''; ?>>
<?php _e('Chars', GKTPLNAME); ?>
</option>
<option value="words"<?php echo (esc_attr($article_text_len_type) == 'words') ? ' selected="selected"' : ''; ?>>
<?php _e('Words', GKTPLNAME); ?>
</option>
</select>
</p>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'article_image_w' ) ); ?>"><?php _e( 'Image size:', GKTPLNAME ); ?></label>

<input id="<?php echo esc_attr( $this->get_field_id( 'article_image_w' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'article_image_w' ) ); ?>" type="text" value="<?php echo esc_attr( $article_image_w ); ?>" class="short" />
&times;
<input id="<?php echo esc_attr( $this->get_field_id( 'article_image_h' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'article_image_h' ) ); ?>" type="text" value="<?php echo esc_attr( $article_image_h ); ?>" class="short" />
</p>

<h3><?php _e('Cache settings', GKTPLNAME); ?></h3>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'cache_time' ) ); ?>"><?php _e( 'Cache time (min):', GKTPLNAME ); ?></label>
<input class="medium" id="<?php echo esc_attr( $this->get_field_id( 'cache_time' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'cache_time' ) ); ?>" type="text" value="<?php echo esc_attr( $cache_time ); ?>" />
</p>

<hr class="clear" />
</div>
<?php
}

/**
*
* Functions used to generate the article elements
*
**/

function generate_art_title($i) {
$art_title = '';
$art_ID = '';
$art_url = '';

if($this->wdgt_config['data_source_type'] == 'post' || $this->wdgt_config['data_source_type'] == 'custom') {
$art_title = $this->wdgt_results[0][$i]->post_title;
$art_ID = $this->wdgt_results[0][$i]->ID;
} else {
$art_title = $this->wdgt_results[$i]->post_title;
$art_ID = $this->wdgt_results[$i]->ID;
}

$art_url = get_permalink($art_ID);
$art_title_short = $this->cut_text('article_title', $art_title);

$output = '<h3 class="gk-ns-header"><a href="'.$art_url.'" title="'.str_replace('"','', $art_title).'">'.$art_title_short.'</a></h3>';

return $output;
}

function generate_art_text($i) {
$art_text = '';

if($this->wdgt_config['data_source_type'] == 'post' || $this->wdgt_config['data_source_type'] == 'custom') {
$art_text = $this->wdgt_results[0][$i]->post_content;
} else {
$art_text = $this->wdgt_results[$i]->post_content;
}

// NEW CODE TO MAKE SHORTCODE SHOW IN WIDGETS
$art_text = apply_filters('the_content', $art_text);
// NEW CODE TO MAKE SHORTCODE SHOW IN WIDGETS

$art_text = $this->cut_text('article_text', $art_text);

$output = '<p class="gk-ns-text">'.$art_text.'</p>';

return $output;
}

function generate_art_image($i) {
$art_ID = '';

if($this->wdgt_config['data_source_type'] == 'post' || $this->wdgt_config['data_source_type'] == 'custom') {
$art_ID = $this->wdgt_results[0][$i]->ID;
} else {
$art_ID = $this->wdgt_results[$i]->ID;
}

$art_url = get_permalink($art_ID);

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $art_ID ), 'single-post-thumbnail' );
$image_path = $image[0];

$upload_dir = wp_upload_dir();
$image_path = str_replace($upload_dir['baseurl'] . '/', '', $image_path);

if($image_path != '') {
$img_editor = wp_get_image_editor( $upload_dir['basedir'] . '/' . $image_path);

if(!is_wp_error($img_editor)) {
$img_editor->resize($this->wdgt_config['article_image_w'], $this->wdgt_config['article_image_h'], true);
$img_filename = $img_editor->generate_filename( $this->id, dirname(__FILE__) . '/' . 'cache_news_slideshow');
$img_editor->save($img_filename);

$new_path = basename($img_filename);
$cache_uri = get_template_directory_uri() . '/gavern/cache_news_slideshow/';

if(is_string($new_path)) {
$new_path = $cache_uri . $new_path;
return '<a href="'.$art_url.'" class="gk-ns-image-link"><img src="'.$new_path.'" alt="" class="gk-ns-image" /></a>';

} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}

/**
*
* Helper functions
*
**/

function cut_text($type, $text, $at_end = '&hellip;') {
// CODE TO BE DELETED TO MAKE SHORTCODE WORK IN WIDGETS $text = strip_tags($text);
$len_type = $this->wdgt_config[$type . '_len_type'];
$len = $this->wdgt_config[$type . '_len'];
$cutter = array();

if($len_type == 'words' && $len > 0){
$temp = explode(' ',$text);

if(count($temp) > $len){
for($i=0; $i<$len; $i++) $cutted[$i] = $temp[$i];
$cutted = implode(' ', $cutted);
$text = $cutted.$at_end;
}
} elseif($len_type == 'words' && $len == 0) {
return '';
} else {
if(function_exists('mb_strlen')) {
if(mb_strlen($text) > $len){
$text = mb_substr($text, 0, $len) . $at_end;
}
} else {
if(strlen($text) > $len){
$text = substr($text, 0, $len) . $at_end;
}
}
}
// replace unnecessary entities at end of the cutted text
$toReplace = array('&&', '&a&', '&am&', '&amp&', '&q&', '&qu&', '&quo&', '&quot&', '&ap&', '&apo&', '&apos&');
$text = str_replace($toReplace, '&', $text);
//
return $text;
}
}

// EOF
User avatar
Fresh Boarder

GK User
Mon Mar 03, 2014 10:22 pm
After editing the code, the appearance of the site changed. I have attached a copy of how it looks.
User avatar
Fresh Boarder

GK User
Tue Mar 04, 2014 1:40 am
ALso - I anm using version 1.4 of the theme. I upgraded from version 1.1 with the expectation that it would make a difference. I tried your suggestions in both versions. It did not make a difference. Neither version worked.
User avatar
Fresh Boarder

GK User
Tue Mar 04, 2014 3:47 am
FYI - I also tried {loadposition rss1) in a test Joomla 2.5 installation. Didn't work in Joomla either.
User avatar
Fresh Boarder

GK User
Tue Mar 04, 2014 8:16 am
Your file seems to to be ok. You can zip your php file and then attach or use code shortcode to paste your code here.

Regarding your issue, it's also don't work with joomla, because it's default behaviour, shortcodes are cut - it's also the same situation with default wordpress post excerpt.

Could you send me a PM with backend access to your website?
User avatar
Moderator


cron