Created
October 29, 2011 20:07
-
-
Save danlamanna/1325012 to your computer and use it in GitHub Desktop.
Making a custom pod widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('widgets_init', 'pods_register_widgets'); | |
/** | |
* Register any/all pods widgets with wordpress. | |
*/ | |
function pods_register_widgets() { | |
register_widget('Pods_Custom_Widget'); | |
} | |
class Pods_Custom_Widget extends WP_Widget { | |
/** | |
* Register Pods Widget | |
*/ | |
public function PodsWidget() { | |
$this->WP_Widget('pods_custom_widget', 'Custom Pod'); | |
} | |
/** | |
* Output of the widget. | |
*/ | |
public function widget($args, $instance) { | |
extract($args); | |
$title = apply_filters('widget_title', $instance['title']); | |
$pod_type = $instance['pod_type']; | |
$template = $instance['template']; | |
$where = $instance['where']; | |
$order = $instance['order']; | |
$limit = $instance['limit']; | |
$podsApi = new PodsAPI(); | |
// Don't display widget if bad data is passed, or they just didn't | |
// select a pod/template. | |
// Perhaps a template_exists function? | |
if (!$podsApi->pod_exists(array('name' => $pod_type)) || | |
!$podsApi->load_template(array('name' => $template))) { | |
return false; | |
} | |
?> | |
<?php echo $before_widget; ?> | |
<?php if (!empty($title)): ?> | |
<?php echo $before_title . $title . $after_title; ?> | |
<?php endif; ?> | |
<?php | |
$podArgs = array('orderby' => ($order) ? $order : 'id DESC'); | |
$limit = ($limit) ? $limit : 15; | |
$where = ($where) ? $where : null; | |
$podObj = new Pod($pod_type); | |
$podObj->find($podArgs, $limit, $where); | |
echo $podObj->template($template); | |
?> | |
<?php echo $after_widget; | |
} | |
/** | |
* Updates the new instance of widget arguments. | |
* @return array $instance Updated instance. | |
*/ | |
public function update($new_instance, $old_instance) { | |
$instance = $old_instance; | |
$instance['title'] = $new_instance['title']; | |
$instance['pod_type'] = $new_instance['pod_type']; | |
$instance['template'] = $new_instance['template']; | |
$instance['where'] = $new_instance['where']; | |
$instance['order'] = $new_instance['order']; | |
$instance['limit'] = $new_instance['limit']; | |
return $instance; | |
} | |
/** | |
* Widget Form. | |
*/ | |
public function form($instance) { | |
$title = esc_attr($instance['title']); | |
$pod_type = esc_attr($instance['pod_type']); | |
$template = esc_attr($instance['template']); | |
$where = esc_attr($instance['where']); | |
$order = esc_attr($instance['order']); | |
$limit = esc_attr($instance['limit']); ?> | |
<?php | |
/* Once dev branch is up, I'll do a print styles and have these | |
* in the ui/css folder. | |
*/ | |
?> | |
<style type="text/css"> | |
label { display:block; } | |
select { width:100%; } | |
</style> | |
<p> | |
<label for="<?php echo $this->get_field_id('title'); ?>"> | |
<?php _e('Title:'); ?> | |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /> | |
</label> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('pod_type'); ?>"> | |
<label><?php _e('Pod:'); ?></label> | |
<?php $podsApi = new PodsAPI(); ?> | |
<?php $existingPods = $podsApi->load_pods(array()); ?> | |
<select name="<?php echo $this->get_field_name('pod_type'); ?>"> | |
<option>Select a Pod</option> | |
<?php foreach ($existingPods as $loadedPod): ?> | |
<?php $selected = ($loadedPod['pod_type'] == $pod_type) ? 'selected' : ''; ?> | |
<option value="<?php echo $loadedPod['pod_type']; ?>" <?php echo $selected; ?>> | |
<?php echo $loadedPod['label']; ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
</label> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_name('template'); ?>"> | |
<label><?php _e('Template:'); ?></label> | |
<select name="<?php echo $this->get_field_name('template'); ?>"> | |
<option>Select a Template</option> | |
<?php // Maybe a load_templates method? Like load_pods ?> | |
<?php $fetchPodTemplates = pods_query("SELECT id, name FROM @wp_pod_templates ORDER BY name"); | |
while ($podTemplate = mysql_fetch_assoc($fetchPodTemplates)): ?> | |
<?php $selected = ($podTemplate['name'] == $template) ? 'selected' : ''; ?> | |
<option value="<?php echo $podTemplate['name']; ?>" <?php echo $selected; ?>> | |
<?php echo $podTemplate['name']; ?> | |
</option> | |
<?php endwhile; ?> | |
</select> | |
</label> | |
</p> | |
<a href="#" onclick="jQuery('div#pods-advanced-options').toggle(); return false;" id="trigger-pods-advanced-options">Advanced Options</a> | |
<div id="pods-advanced-options" style="display:none;"> | |
<p> | |
<label for="<?php echo $this->get_field_name('where'); ?>"> | |
<label><?php _e('Where:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('where'); ?>" name="<?php echo $this->get_field_name('where'); ?>" type="text" value="<?php echo $where; ?>" /> | |
</label> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_name('order'); ?>"> | |
<label><?php _e('Order:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" type="text" value="<?php echo $order; ?>" /> | |
</label> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_name('limit'); ?>"> | |
<label><?php _e('Limit:'); ?></label> | |
<input size="3" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /> | |
</label> | |
</p> | |
</div> | |
<?php | |
} // function form() | |
} // class Pods_Custom_Widget |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment