Skip to content

Instantly share code, notes, and snippets.

@vicchi
Created May 9, 2012 07:58
Show Gist options
  • Save vicchi/2642811 to your computer and use it in GitHub Desktop.
Save vicchi/2642811 to your computer and use it in GitHub Desktop.
WordPress Loop Processing Widget Proof Of Concept
<?php
class WPPluginTemplateWidget extends WP_Widget {
function __construct () {
$widget_ops = array (
'description' => __('Template Test Widget')
);
parent::WP_Widget ('WPPluginTemplateWidget', __('WP Plugin Template'), $widget_ops);
}
function form ($instance) {
$text_stub = '<label for="%s">%s</label><input type="text" id="%s" name="%s" value="%s" class="widefat" />';
$instance = wp_parse_args (
(array) $instance,
array (
'title' => __('WP Plugin Template')
)
);
$content = '<p>'
. sprintf ($text_stub,
$this->get_field_id ('title'),
__('Widget Title'),
$this->get_field_id ('title'),
$this->get_field_name ('title'),
attribute_escape ($instance['title'])
)
. '</p>';
echo $content;
}
function update ($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags ($new_instance['title']);
return $instance;
}
function widget ($args, $instance) {
global $post;
extract ($args, EXTR_SKIP);
$content = $before_widget;
if ($instance['title']) {
$content .= $before_title . $instance['title'] . $after_title;
}
if (!is_main_query ()) {
wp_reset_query ();
}
if (is_front_page ()) {
$content .= '<strong>Type: Front Page</strong><br />';
}
elseif (is_archive ()) {
$content .= '<strong>Type: Archive</strong><br />';
}
elseif (is_page ()) {
$content .= '<strong>Type: Page</strong><br />';
}
elseif (is_single ()) {
$content .= '<strong>Type: Single</strong><br />';
}
elseif (is_feed ()) {
$content .= '<strong>Type: Feed</strong><br />';
}
else {
$content .= '<strong>Type: Unknown</strong><br />';
}
while (have_posts ()) : the_post ();
$user = get_userdata ($post->post_author);
$content .= $post->post_title . ' (<em>' . $user->user_login . '</em>)<br />';
endwhile;
$content .= $after_widget;
echo $content;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment