Convert Widget Code to Function
-
I have a widget that will create monthly or yearly archives for CPT’s in WP. I would like to take that widget function and use it to add a date filter to the page located here:
http://www.host4eighty5.com/sermon/
In effect, the widget just needs to work as a drop down in place of the current one. I’m attacking the widget files for consideration. The specific CPT that will need to filter out is called ‘tz_sermon’
<?php /** * @package CPT_Archive * @version 0.1 */ /* Plugin Name: Custom Post Type Archive Description: Adds an Archive Widget that uses Custom Post TYpes Author: Brenda Egeland Version: 0.1 Author URI: http://www.redletterdesign.net/ */ /** * Archives widget class, taken from core and modified for custom post type * * @since 2.8.0 */ class CPT_Archives extends WP_Widget { function CPT_Archives() { $widget_ops = array('classname' => 'widget_archive', 'description' => __( 'A monthly archive of your site’s custom post type') ); $this->WP_Widget('cpt_archives', __('CPT Archives'), $widget_ops); } function widget( $args, $instance ) { extract($args); $c = $instance['count'] ? '1' : '0'; $d = $instance['dropdown'] ? '1' : '0'; $title = apply_filters('widget_title', empty($instance['title']) ? __('Archives') : $instance['title'], $instance, $this->id_base); $cpt = apply_filters('cpt', empty($instance['cpt']) ? __('post') : $instance['cpt'], $instance, $this->id_base); $type = apply_filters('type', empty($instance['type']) ? __('monthly') : $instance['type'], $instance, $this->id_base); echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; if ( $d ) { ?> <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__('Select Month')); ?></option> <?php wp_get_archives(apply_filters('widget_archives_dropdown_args', array('type' => 'monthly', 'format' => 'option', 'show_post_count' => $c))); ?> </select> <?php } else { ?> <ul class="monkey"> <?php wp_get_cpt_archives(apply_filters('widget_cpt_archives_args', array('type' => $type, 'show_post_count' => $c, 'cpt' => $cpt))); ?> </ul> <?php } echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '', 'cpt' => 'post') ); $instance['title'] = strip_tags($new_instance['title']); $instance['count'] = $new_instance['count'] ? 1 : 0; $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0; $instance['cpt'] = strip_tags($new_instance['cpt']); $instance['type'] = strip_tags($new_instance['type']); return $instance; } function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '', 'cpt' => 'post', 'type' => 'monthly') ); $title = strip_tags($instance['title']); $count = $instance['count'] ? 'checked="checked"' : ''; $dropdown = $instance['dropdown'] ? 'checked="checked"' : ''; $cpt= strip_tags($instance['cpt']); $type= strip_tags($instance['type']); ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> <p><label for="<?php echo $this->get_field_id('cpt'); ?>"><?php _e('Post Type:'); ?></label> <select id="<?php echo $this->get_field_id('cpt'); ?>" name="<?php echo $this->get_field_name('cpt'); ?>"> <?php $post_types=get_post_types('','names'); $badtypes=array('nav_menu_item','revision','attachment'); // others???? foreach ($post_types as $ptype ) { if (!in_array($ptype,$badtypes)) { echo "<option value=\"$ptype\" "; if ($cpt==$ptype) echo 'selected="true"'; echo ">$ptype</option>"; } } ?> </select> <p><label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Monthly, Yearly:'); ?></label> <select id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>"> <option value="monthly" <?php echo ($type=='monthly') ? 'selected="selected"' : '';?>>Monthly</option> <option value="yearly" <?php echo ($type=='yearly') ? 'selected="selected"' : '';?>>Yearly</option> </select> <p> <input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label> <br/> <input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label> </p> <?php } } function CPT_Archives_widget_init() { if ( !is_blog_installed() ) return; register_widget('CPT_Archives'); do_action('widgets_init'); } add_action('init', 'CPT_Archives_widget_init', 1); // MODIFIED VERSION FOR CUSTOM POST TYPES /** * Display archive links based on type and format AND CUSTOM POST TYPE. * * The 'type' argument offers a few choices and by default will display monthly * archive links. The other options for values are 'daily', 'weekly', 'monthly', * 'yearly', 'postbypost' or 'alpha'. Both 'postbypost' and 'alpha' display the * same archive link list, the difference between the two is that 'alpha' * will order by post title and 'postbypost' will order by post date. * * The date archives will logically display dates with links to the archive post * page. The 'postbypost' and 'alpha' values for 'type' argument will display * the post titles. * * The 'limit' argument will only display a limited amount of links, specified * by the 'limit' integer value. By default, there is no limit. The * 'show_post_count' argument will show how many posts are within the archive. * By default, the 'show_post_count' argument is set to false. * * For the 'format', 'before', and 'after' arguments, see {@link * get_archives_link()}. The values of these arguments have to do with that * function. * * @since 1.2.0 * * @param string|array $args Optional. Override defaults. */ function wp_get_cpt_archives($args = '') { global $wpdb, $wp_locale; $defaults = array( 'type' => 'monthly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'cpt' => 'post' ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); if ( '' == $type ) $type = 'monthly'; if ( '' != $limit ) { $limit = absint($limit); $limit = ' LIMIT '.$limit; } // this is what will separate dates on weekly archive links $archive_week_separator = '–'; // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride $archive_date_format_over_ride = 0; // options for daily archive (only if you over-ride the general date format) $archive_day_date_format = 'Y/m/d'; // options for weekly archive (only if you over-ride the general date format) $archive_week_start_date_format = 'Y/m/d'; $archive_week_end_date_format = 'Y/m/d'; if ( !$archive_date_format_over_ride ) { $archive_day_date_format = get_option('date_format'); $archive_week_start_date_format = get_option('date_format'); $archive_week_end_date_format = get_option('date_format'); } //filters $where = apply_filters('getarchives_where', "WHERE post_type = '" . $cpt . "' AND post_status = 'publish'", $r ); $join = apply_filters('getarchives_join', "", $r); $output = ''; $cpt_query_var = '?post_type=' . $cpt; if ( 'monthly' == $type ) { $query = "SELECT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit"; $key = md5($query); $cache = wp_cache_get( 'wp_get_cpt_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_set( 'wp_get_cpt_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } if ( $arcresults ) { $afterafter = $after; foreach ( (array) $arcresults as $arcresult ) { $url = get_month_link( $arcresult->year, $arcresult->month ) . $cpt_query_var; /* translators: 1: month name, 2: 4-digit year */ $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year); if ( $show_post_count ) $after = ' ('.$arcresult->posts.')' . $afterafter; $output .= get_archives_link($url, $text, $format, $before, $after); } } } elseif ('yearly' == $type) { $query = "SELECT YEAR(post_date) AS <code>year</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $limit"; $key = md5($query); $cache = wp_cache_get( 'wp_get_cpt_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_set( 'wp_get_cpt_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } if ($arcresults) { $afterafter = $after; foreach ( (array) $arcresults as $arcresult) { $url = get_year_link($arcresult->year) . $cpt_query_var; $text = sprintf('%d', $arcresult->year); if ($show_post_count) $after = ' ('.$arcresult->posts.')' . $afterafter; $output .= get_archives_link($url, $text, $format, $before, $after); } } } elseif ( 'daily' == $type ) { $query = "SELECT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, DAYOFMONTH(post_date) AS <code>dayofmonth</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit"; $key = md5($query); $cache = wp_cache_get( 'wp_get_cpt_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_set( 'wp_get_cpt_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } if ( $arcresults ) { $afterafter = $after; foreach ( (array) $arcresults as $arcresult ) { $url = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth) . $cpt_query_var; $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth); $text = mysql2date($archive_day_date_format, $date); if ($show_post_count) $after = ' ('.$arcresult->posts.')'.$afterafter; $output .= get_archives_link($url, $text, $format, $before, $after); } } } elseif ( 'weekly' == $type ) { $week = _wp_mysql_week( '<code>post_date</code>' ); $query = "SELECT DISTINCT $week AS <code>week</code>, YEAR( <code>post_date</code> ) AS <code>yr</code>, DATE_FORMAT( <code>post_date</code>, '%Y-%m-%d' ) AS <code>yyyymmdd</code>, count( <code>ID</code> ) AS <code>posts</code> FROM <code>$wpdb->posts</code> $join $where GROUP BY $week, YEAR( <code>post_date</code> ) ORDER BY <code>post_date</code> DESC $limit"; $key = md5($query); $cache = wp_cache_get( 'wp_get_cpt_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_set( 'wp_get_cpt_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } $arc_w_last = ''; $afterafter = $after; if ( $arcresults ) { foreach ( (array) $arcresults as $arcresult ) { if ( $arcresult->week != $arc_w_last ) { $arc_year = $arcresult->yr; $arc_w_last = $arcresult->week; $arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week')); $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']); $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']); $url = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $arcresult->week) . $cpt_query_var; $text = $arc_week_start . $archive_week_separator . $arc_week_end; if ($show_post_count) $after = ' ('.$arcresult->posts.')'.$afterafter; $output .= get_archives_link($url, $text, $format, $before, $after); } } } } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) { $orderby = ('alpha' == $type) ? "post_title ASC " : "post_date DESC "; $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"; $key = md5($query); $cache = wp_cache_get( 'wp_get_cpt_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_set( 'wp_get_cpt_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } if ( $arcresults ) { foreach ( (array) $arcresults as $arcresult ) { if ( $arcresult->post_date != '0000-00-00 00:00:00' ) { $url = get_permalink($arcresult) . $cpt_query_var; $arc_title = $arcresult->post_title; if ( $arc_title ) $text = strip_tags(apply_filters('the_title', $arc_title)); else $text = $arcresult->ID; $output .= get_archives_link($url, $text, $format, $before, $after); } } } } if ( $echo ) echo $output; else return $output; }
The topic ‘Convert Widget Code to Function’ is closed to new replies.