| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Custom Post Widget |
| 4 |
Plugin URI: http://www.vanderwijk.com/services/web-design/wordpress-custom-post-widget/ |
| 5 |
Description: Show the content of a custom post of the type 'content_block' in a widget. |
| 6 |
Version: 1.0 |
| 7 |
Author: Johan van der Wijk |
| 8 |
Author URI: http://www.vanderwijk.com |
| 9 |
|
| 10 |
Release notes: 1.0 First version |
| 11 |
|
| 12 |
*/ |
| 13 |
|
| 14 |
// First create the widget for the admin panel |
| 15 |
class custom_post_widget extends WP_Widget |
| 16 |
{ |
| 17 |
function custom_post_widget() |
| 18 |
{ |
| 19 |
$widget_ops = array('description' => __('Displays custom post content in a widget')); |
| 20 |
$this->WP_Widget('custom_post_widget', __('Content Block'), $widget_ops); |
| 21 |
} |
| 22 |
|
| 23 |
function form($instance) |
| 24 |
{ |
| 25 |
$custom_post_id = esc_attr($instance['custom_post_id']); |
| 26 |
|
| 27 |
?> |
| 28 |
<p> |
| 29 |
<label for="<?php echo $this->get_field_id('custom_post_id'); ?>"> <?php echo __('Content Block to Display:') ?> |
| 30 |
<select id="<?php echo $this->get_field_id('custom_post_id'); ?>" name="<?php echo $this->get_field_name('custom_post_id'); ?>"> |
| 31 |
<?php query_posts('post_type=content_block&orderby=ID&order=ASC'); |
| 32 |
if ( have_posts() ) : while ( have_posts() ) : the_post(); |
| 33 |
$currentID = get_the_ID(); |
| 34 |
if($currentID == $custom_post_id) |
| 35 |
$extra = 'selected'; |
| 36 |
else |
| 37 |
$extra = ''; |
| 38 |
echo '<option value="'.$currentID.'" '.$extra.'>'.get_the_title().'</option>'; |
| 39 |
endwhile; else: |
| 40 |
echo '<option value="empty">No content blocks available</option>'; |
| 41 |
endif; |
| 42 |
wp_reset_query(); ?> |
| 43 |
</select> |
| 44 |
</label> |
| 45 |
</p><?php |
| 46 |
} |
| 47 |
|
| 48 |
function update($new_instance, $old_instance) |
| 49 |
{ |
| 50 |
$instance = $old_instance; |
| 51 |
$instance['custom_post_id'] = strip_tags($new_instance['custom_post_id']); |
| 52 |
return $instance; |
| 53 |
} |
| 54 |
|
| 55 |
function widget($args, $instance) |
| 56 |
{ |
| 57 |
extract($args); |
| 58 |
|
| 59 |
$custom_post_id = ( $instance['custom_post_id'] != '' ) ? esc_attr($instance['custom_post_id']) : 'Zoeken'; |
| 60 |
|
| 61 |
// Output title & $before_widget |
| 62 |
echo $title . $before_widget; |
| 63 |
|
| 64 |
// Output the query to find the custom post |
| 65 |
query_posts( 'post_type=content_block&p=' . $custom_post_id ); |
| 66 |
while (have_posts()) : the_post(); |
| 67 |
echo the_content(); |
| 68 |
endwhile; |
| 69 |
wp_reset_query(); |
| 70 |
|
| 71 |
// Output $after_widget |
| 72 |
echo $after_widget; |
| 73 |
} |
| 74 |
} |
| 75 |
add_action('widgets_init', create_function('', 'return register_widget("custom_post_widget");')); |
| 76 |
|
| 77 |
// Create the Content Block custom post type |
| 78 |
|
| 79 |
add_action('init', 'my_content_block_post_type_init'); |
| 80 |
|
| 81 |
function my_content_block_post_type_init() |
| 82 |
{ |
| 83 |
$labels = array( |
| 84 |
'name' => _x('Content Blocks', 'post type general name'), |
| 85 |
'singular_name' => _x('Content Block', 'post type singular name'), |
| 86 |
'add_new' => _x('Add Content Block', 'block'), |
| 87 |
'add_new_item' => __('Add New Content Block'), |
| 88 |
'edit_item' => __('Edit Content Block'), |
| 89 |
'new_item' => __('New Content Block'), |
| 90 |
'view_item' => __('View Content Block'), |
| 91 |
'search_items' => __('Search Content Blocks'), |
| 92 |
'not_found' => __('No Content Blocks Found'), |
| 93 |
'not_found_in_trash' => __('No Content Blocks found in Trash'), |
| 94 |
'parent_item_colon' => '' |
| 95 |
); |
| 96 |
$options = array( |
| 97 |
'labels' => $labels, |
| 98 |
'public' => false, |
| 99 |
'publicly_queryable' => false, |
| 100 |
'exclude_from_search' => true, |
| 101 |
'show_ui' => true, |
| 102 |
'query_var' => true, |
| 103 |
'rewrite' => true, |
| 104 |
'capability_type' => 'post', |
| 105 |
'hierarchical' => false, |
| 106 |
'menu_position' => null, |
| 107 |
'supports' => array('title','editor','revisions','thumbnail') |
| 108 |
); |
| 109 |
register_post_type('content_block',$options); |
| 110 |
} |
| 111 |
|
| 112 |
add_filter('post_updated_messages', 'content_block_messages'); |
| 113 |
|
| 114 |
function content_block_messages( $messages ) { |
| 115 |
|
| 116 |
$messages['content_block'] = array( |
| 117 |
0 => '', |
| 118 |
1 => sprintf( __('Content Block updated. <a href="%s">View Content Block</a>'), esc_url( get_permalink($post_ID) ) ), |
| 119 |
2 => __('Custom field updated.'), |
| 120 |
3 => __('Custom field deleted.'), |
| 121 |
4 => __('Content Block updated.'), |
| 122 |
5 => isset($_GET['revision']) ? sprintf( __('Content Block restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
| 123 |
6 => sprintf( __('Content Block published. <a href="%s">View Content Block</a>'), esc_url( get_permalink($post_ID) ) ), |
| 124 |
7 => __('Block saved.'), |
| 125 |
8 => sprintf( __('Content Block submitted. <a target="_blank" href="%s">Preview Content Block</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), |
| 126 |
9 => sprintf( __('Content Block scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview block</a>'), |
| 127 |
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), |
| 128 |
10 => sprintf( __('Content Block draft updated. <a target="_blank" href="%s">Preview Content Block</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), |
| 129 |
); |
| 130 |
|
| 131 |
return $messages; |
| 132 |
} |
| 133 |
|
| 134 |
?> |