| 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.3 |
| 7 |
Author: Johan van der Wijk |
| 8 |
Author URI: http://www.vanderwijk.com |
| 9 |
|
| 10 |
Release notes: 1.3 Now the title of the content block is displayed in the admin widget |
| 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 |
$show_custom_post_title = isset($instance['show_custom_post_title ']) ? $instance['show_custom_post_title '] : true; |
| 27 |
|
| 28 |
?> |
| 29 |
<p> |
| 30 |
<label for="<?php echo $this->get_field_id('custom_post_id'); ?>"> <?php echo __('Content Block to Display:') ?> |
| 31 |
<select class="widefat" id="<?php echo $this->get_field_id('custom_post_id'); ?>" name="<?php echo $this->get_field_name('custom_post_id'); ?>"> |
| 32 |
<?php query_posts('post_type=content_block&orderby=ID&order=ASC&showposts=-1'); |
| 33 |
if ( have_posts() ) : while ( have_posts() ) : the_post(); |
| 34 |
$currentID = get_the_ID(); |
| 35 |
if($currentID == $custom_post_id) |
| 36 |
$extra = 'selected' and |
| 37 |
$widgetExtraTitle = get_the_title(); |
| 38 |
else |
| 39 |
$extra = ''; |
| 40 |
echo '<option value="'.$currentID.'" '.$extra.'>'.get_the_title().'</option>'; |
| 41 |
endwhile; else: |
| 42 |
echo '<option value="empty">No content blocks available</option>'; |
| 43 |
endif; |
| 44 |
?> |
| 45 |
</select> |
| 46 |
</label> |
| 47 |
</p> |
| 48 |
<?php ?> |
| 49 |
<input type="hidden" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $widgetExtraTitle; ?>" /> |
| 50 |
<?php wp_reset_query(); ?> |
| 51 |
|
| 52 |
<p> |
| 53 |
<input class="checkbox" type="checkbox" <?php checked( (bool) $instance['show_custom_post_title'], true ); ?> id="<?php echo $this->get_field_id( 'show_custom_post_title' ); ?>" name="<?php echo $this->get_field_name( 'show_custom_post_title' ); ?>" /> |
| 54 |
<label for="<?php echo $this->get_field_id( 'show_custom_post_title' ); ?>"><?php echo __('Show Post Title') ?></label> |
| 55 |
</p> |
| 56 |
|
| 57 |
<?php |
| 58 |
} |
| 59 |
|
| 60 |
function update($new_instance, $old_instance) |
| 61 |
{ |
| 62 |
$instance = $old_instance; |
| 63 |
$instance['custom_post_id'] = strip_tags($new_instance['custom_post_id']); |
| 64 |
$instance['show_custom_post_title'] = $new_instance['show_custom_post_title']; |
| 65 |
|
| 66 |
return $instance; |
| 67 |
} |
| 68 |
|
| 69 |
function widget($args, $instance) |
| 70 |
{ |
| 71 |
extract($args); |
| 72 |
|
| 73 |
$custom_post_id = ( $instance['custom_post_id'] != '' ) ? esc_attr($instance['custom_post_id']) : 'Zoeken'; |
| 74 |
|
| 75 |
/* Our variables from the widget settings. */ |
| 76 |
$show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : false; |
| 77 |
|
| 78 |
/* Before widget (defined by themes). */ |
| 79 |
echo $before_widget; |
| 80 |
|
| 81 |
// Output the query to find the custom post |
| 82 |
query_posts( 'post_type=content_block&p=' . $custom_post_id ); |
| 83 |
while (have_posts()) : the_post(); |
| 84 |
|
| 85 |
if ( $show_custom_post_title ) |
| 86 |
echo the_title($before_title, $after_title); // This is the line that displays the title |
| 87 |
|
| 88 |
echo the_content(); |
| 89 |
endwhile; |
| 90 |
wp_reset_query(); |
| 91 |
|
| 92 |
// Output $after_widget |
| 93 |
echo $after_widget; |
| 94 |
} |
| 95 |
} |
| 96 |
add_action('widgets_init', create_function('', 'return register_widget("custom_post_widget");')); |
| 97 |
|
| 98 |
// Create the Content Block custom post type |
| 99 |
|
| 100 |
add_action('init', 'my_content_block_post_type_init'); |
| 101 |
|
| 102 |
function my_content_block_post_type_init() |
| 103 |
{ |
| 104 |
$labels = array( |
| 105 |
'name' => _x('Content Blocks', 'post type general name'), |
| 106 |
'singular_name' => _x('Content Block', 'post type singular name'), |
| 107 |
'add_new' => _x('Add Content Block', 'block'), |
| 108 |
'add_new_item' => __('Add New Content Block'), |
| 109 |
'edit_item' => __('Edit Content Block'), |
| 110 |
'new_item' => __('New Content Block'), |
| 111 |
'view_item' => __('View Content Block'), |
| 112 |
'search_items' => __('Search Content Blocks'), |
| 113 |
'not_found' => __('No Content Blocks Found'), |
| 114 |
'not_found_in_trash' => __('No Content Blocks found in Trash'), |
| 115 |
'parent_item_colon' => '' |
| 116 |
); |
| 117 |
$options = array( |
| 118 |
'labels' => $labels, |
| 119 |
'public' => false, |
| 120 |
'publicly_queryable' => false, |
| 121 |
'exclude_from_search' => true, |
| 122 |
'show_ui' => true, |
| 123 |
'query_var' => true, |
| 124 |
'rewrite' => true, |
| 125 |
'capability_type' => 'post', |
| 126 |
'hierarchical' => false, |
| 127 |
'menu_position' => null, |
| 128 |
'supports' => array('title','editor','revisions','thumbnail') |
| 129 |
); |
| 130 |
register_post_type('content_block',$options); |
| 131 |
} |
| 132 |
|
| 133 |
add_filter('post_updated_messages', 'content_block_messages'); |
| 134 |
|
| 135 |
function content_block_messages( $messages ) { |
| 136 |
|
| 137 |
$messages['content_block'] = array( |
| 138 |
0 => '', |
| 139 |
1 => sprintf( __('Content Block updated. <a href="%s">View Content Block</a>'), esc_url( get_permalink($post_ID) ) ), |
| 140 |
2 => __('Custom field updated.'), |
| 141 |
3 => __('Custom field deleted.'), |
| 142 |
4 => __('Content Block updated.'), |
| 143 |
5 => isset($_GET['revision']) ? sprintf( __('Content Block restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
| 144 |
6 => sprintf( __('Content Block published. <a href="%s">View Content Block</a>'), esc_url( get_permalink($post_ID) ) ), |
| 145 |
7 => __('Block saved.'), |
| 146 |
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) ) ) ), |
| 147 |
9 => sprintf( __('Content Block scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview block</a>'), |
| 148 |
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), |
| 149 |
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) ) ) ), |
| 150 |
); |
| 151 |
|
| 152 |
return $messages; |
| 153 |
} |
| 154 |
|
| 155 |
?> |