ID;
$widget_title = '';
$widget_tags = '';
if ($post_id > 0) {
$posts_by_tag_page_fields = get_post_meta($post_id, 'posts_by_tag_page_fields', TRUE);
if (isset($posts_by_tag_page_fields) && is_array($posts_by_tag_page_fields)) {
$widget_title = $posts_by_tag_page_fields['widget_title'];
$widget_tags = $posts_by_tag_page_fields['widget_tags'];
}
}
// Use nonce for verification
?>
$attributes
*/
function shortcode_handler($attributes) {
extract(shortcode_atts(array(
"tags" => '', // comma Separated list of tags
"number" => '5',
"exclude" => FALSE,
"exclude_current_post" => FALSE,
"excerpt" => FALSE,
"content" => FALSE,
'thumbnail' => FALSE,
'order_by' => 'date',
'order' => 'desc',
'author' => FALSE,
'date' => FALSE,
'tag_links' => FALSE
), $attributes));
if (strtolower($exclude) == "false") {
$exclude = FALSE;
}
if (strtolower($exclude_current_post) == "false") {
$exclude_current_post = FALSE;
}
if (strtolower($excerpt) == "false") {
$excerpt = FALSE;
}
if (strtolower($thumbnail) == "false") {
$thumbnail = FALSE;
}
if (strtolower($author) == "false") {
$author = FALSE;
}
if (strtolower($date) == "false") {
$date = FALSE;
}
if (strtolower($content) == "false") {
$content = FALSE;
}
if (strtolower($tag_links) == "false") {
$tag_links = FALSE;
}
// call the template function
$output = get_posts_by_tag($tags, $number, $exclude, $excerpt, $thumbnail, $order_by, $order, $author, $date, $content, $exclude_current_post);
if ($tag_links && !$exclude) {
$output .= get_tag_more_links($tags);
}
return $output;
}
// PHP4 compatibility
function PostsByTag() {
$this->__construct();
}
}
// Start this plugin once all other plugins are fully loaded
add_action( 'init', 'PostsByTag' ); function PostsByTag() { global $PostsByTag; $PostsByTag = new PostsByTag(); }
// register TagWidget widget
add_action('widgets_init', create_function('', 'return register_widget("TagWidget");'));
/**
* TagWidget Class
*/
class TagWidget extends WP_Widget {
/** constructor */
function TagWidget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'TagWidget', 'description' => __('Widget that shows posts from a set of tags', 'posts-by-tag'));
/* Widget control settings. */
$control_ops = array('id_base' => 'tag-widget' );
/* Create the widget. */
parent::WP_Widget( 'tag-widget', __('Posts By Tag', 'posts-by-tag'), $widget_ops, $control_ops );
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
global $post;
extract( $args );
$tags = $instance['tags'];
$current_tags = (bool) $instance['current_tags'];
$current_page_tags = (bool) $instance['current_page_tags'];
$number = $instance['number']; // Number of posts to show.
$exclude = (bool) $instance['exclude'];
$exclude_current_post = (bool) $instance['exclude_current_post'];
$excerpt = (bool) $instance['excerpt'];
$content = (bool) $instance['content'];
$thumbnail = (bool) $instance['thumbnail'];
$order_by = $instance['order_by'];
$order = $instance['order'];
$author = (bool) $instance['author'];
$date = (bool) $instance['date'];
$tag_links = (bool) $instance['tag_links'];
$title = $instance['title'];
$post_id = $post->ID;
if ($current_tags) {
// if current tags is enabled then set tags to empty
$tags = '';
}
if ($current_page_tags) {
$tags = ''; // reset the tags
// get tags and title from page custom fields
if ($post_id > 0) {
$posts_by_tag_page_fields = get_post_meta($post_id, 'posts_by_tag_page_fields', TRUE);
if (isset($posts_by_tag_page_fields) && is_array($posts_by_tag_page_fields)) {
if ($posts_by_tag_page_fields['widget_title'] != '') {
$title = $posts_by_tag_page_fields['widget_title'];
}
if ($posts_by_tag_page_fields['widget_tags'] != '') {
$tags = $posts_by_tag_page_fields['widget_tags'];
}
}
}
}
if (($current_tags || $current_page_tags) && !is_singular()) {
// either current post tags or page custom tags is enabled and it is not a singular page
$widget_content = '';
} else {
if ($current_page_tags && $tags == '') {
$widget_content = '';
} else {
if (($current_tags || $current_page_tags) && is_singular() && $post_id > 0) {
$key = "posts-by-tag-$widget_id-$post_id";
} else {
$key = "posts-by-tag-$widget_id";
}
if ( false === ( $widget_content = get_transient( $key ) ) ) {
$widget_content = get_posts_by_tag($tags, $number, $exclude, $excerpt, $thumbnail, $order_by, $order, $author, $date, $content, $exclude_current_post);
// store in cache
set_transient($key, $widget_content, 86400); // 60*60*24 - 1 Day
}
}
}
if ($widget_content != '') {
echo $before_widget;
echo $before_title;
echo $title;
echo $after_title;
echo $widget_content;
if ($tag_links && !$exclude) {
echo get_tag_more_links($tags);
}
echo $after_widget;
}
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
// validate data
$instance['title'] = strip_tags($new_instance['title']);
$instance['tags'] = strip_tags($new_instance['tags']);
$instance['current_tags'] = (bool)$new_instance['current_tags'];
$instance['current_page_tags'] = (bool)$new_instance['current_page_tags'];
$instance['number'] = intval($new_instance['number']);
$instance['exclude'] = (bool)$new_instance['exclude'];
$instance['exclude_current_post'] = (bool)$new_instance['exclude_current_post'];
$instance['thumbnail'] = (bool)$new_instance['thumbnail'];
$instance['author'] = (bool)$new_instance['author'];
$instance['date'] = (bool)$new_instance['date'];
$instance['excerpt'] = (bool)$new_instance['excerpt'];
$instance['content'] = (bool)$new_instance['content'];
$instance['order'] = ($new_instance['order'] === 'asc') ? 'asc' : 'desc';
$instance['order_by'] = ($new_instance['order_by'] === 'date') ? 'date' : 'title';
$instance['tag_links'] = (bool)$new_instance['tag_links'];
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
/* Set up some default widget settings. */
$defaults = array( 'title' => '', 'tags' => '', 'current_tags' => FALSE, 'number' => '5', 'exclude' => FALSE, 'exclude_current_post' => FALSE, 'thumbnail' => FALSE, 'author' => FALSE, 'date' => FALSE, 'excerpt' => FALSE, 'content' => FALSE);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = esc_attr($instance['title']);
$tags = $instance['tags'];
$number = intval($instance['number']);
$current_tags = (bool) $instance['current_tags'];
$current_page_tags = (bool) $instance['current_page_tags'];
$exclude = (bool) $instance['exclude'];
$exclude_current_post = (bool) $instance['exclude_current_post'];
$thumbnail = (bool) $instance['thumbnail'];
$author = (bool) $instance['author'];
$date = (bool) $instance['date'];
$excerpt = (bool) $instance['excerpt'];
$content = (bool) $instance['content'];
$order = ( strtolower( $instance['order'] ) === 'asc' ) ? 'asc' : 'desc';
$order_by = ( strtolower( $instance['order_by'] ) === 'date' ) ? 'date' : 'title';
$tag_links = (bool) $instance['tag_links'];
?>
$tags
* @param $number Number of posts to show
* @param $exclude Whether to exclude the tags specified. Default is FALSE
* @param $excerpt
* @param $thumbnail
* @param $order_by (title, date) defaults to 'date'
* @param $order (asc, desc) defaults to 'desc'
* @param $author - Whether to show the author name or not
* @param $date - Whether to show the post date or not
* @param $content
* @param $exclude_current_post Whether to exclude the current post/page. Default is FALSE
* @param $tag_links Whether to display tag links at the end
*/
function posts_by_tag($tags, $number, $exclude = FALSE, $excerpt = FALSE, $thumbnail = FALSE, $order_by = 'date', $order = 'desc', $author = FALSE, $date = FALSE, $content = FALSE, $exclude_current_post = FALSE, $tag_links = FALSE) {
$output = get_posts_by_tag($tags, $number, $exclude, $excerpt, $thumbnail, $order_by, $order, $author, $date, $content, $exclude_current_post);
if ($tag_links && !$exclude) {
$output .= get_tag_more_links($tags);
}
echo $output;
}
/**
* Helper function for posts_by_tag
*
* @param $tags
* @param $number Number of posts to show
* @param $exclude Whether to exclude the tags specified. Default is FALSE
* @param $excerpt
* @param $thumbnail
* @param $order_by (title, date) defaults to 'date'
* @param $order (asc, desc) defaults to 'desc'
* @param $author - Whether to show the author name or not
* @param $date - Whether to show the post date or not
* @param $content - Whether to show post content or not
* @param $exclude_current_post Whether to exclude the current post/page. Default is FALSE
*/
function get_posts_by_tag($tags, $number, $exclude = FALSE, $excerpt = FALSE, $thumbnail = FALSE, $order_by = 'date', $order = 'desc', $author = FALSE, $date = FALSE, $content = FALSE, $exclude_current_post = FALSE) {
global $wp_query;
$current_post_id = $wp_query->post->ID;
$output = '';
$tag_id_array = array();
if ($tags == '') {
// if tags is empty then take from current posts
if (is_single()) {
$tag_array = wp_get_post_tags($current_post_id);
foreach ($tag_array as $tag) {
$tag_id_array[] = $tag->term_id;
}
}
} else {
// Get array of post info.
$tag_array = explode(",", $tags);
foreach ($tag_array as $tag) {
$tag_id_array[] = get_tag_ID(trim($tag));
}
}
if (count($tag_id_array) > 0) {
// only if we have atleast one tag. get_posts has a bug. If empty array is passed, it returns all posts. That's why we need this condition
$tag_arg = 'tag__in';
if ($exclude) {
$tag_arg = 'tag__not_in';
}
// saving the query
$temp_query = clone $wp_query;
// TODO: Need to cache this.
$tag_posts = get_posts( array( 'numberposts' => $number, $tag_arg => $tag_id_array, 'order_by' => $order_by, 'order' => $order ) );
// restoring the query so it can be later used to display our posts
$wp_query = clone $temp_query;
if (count($tag_posts) > 0) {
$output = '
';
}
}
return $output;
}
/**
* Get tag more links for a bunch of tags. Exposed as a template function
*
* @param $tags
* @param $prefix
*/
function get_tag_more_links($tags, $prefix = 'More posts: ') {
global $wp_query;
$tag_array = array();
$output = '';
if ($tags == '') {
// if tags is empty then take from current posts
if (is_single()) {
$tag_array = wp_get_post_tags($wp_query->post->ID);
}
} else {
$tag_array = explode(",", $tags);
}
if (count($tag_array) > 0) {
$output = '
';
}
return $output;
}
/**
* Get tag more link for a single tag
*
* @param $tag
* @return
*/
function get_tag_more_link($tag) {
return '' . $tag . ' ';
}
/**
* get tag id from tag name
*
* @param $tag_name
* @return term id. 0 if not found
*/
if (!function_exists("get_tag_ID")) {
function get_tag_ID($tag_name) {
$tag = get_term_by('name', $tag_name, 'post_tag');
if ($tag) {
return $tag->term_id;
} else {
return 0;
}
}
}
/**
* Get the content of a post with formatting.
* Calls the filter before returning the content
*
*/
function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
?>