Advertisement
vanderwijk

Show latest content block

Sep 29th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. <?php
  2.  
  3. // First create the widget for the admin panel
  4. class custom_post_widget extends WP_Widget {
  5. function custom_post_widget() {
  6. $widget_ops = array('description' => __('Displays custom post content in a widget', 'custom-post-widget'));
  7. $this->WP_Widget('custom_post_widget', __('Content Block', 'custom-post-widget'), $widget_ops);
  8. }
  9.  
  10. function form($instance) {
  11. $custom_post_id = esc_attr($instance['custom_post_id']);
  12. $show_custom_post_title = isset($instance['show_custom_post_title ']) ? $instance['show_custom_post_title '] : true; ?>
  13.  
  14. <p>
  15. <label for="<?php echo $this->get_field_id('custom_post_id'); ?>"> <?php echo __('Content Block to Display:', 'custom-post-widget') ?>
  16. <select class="widefat" id="<?php echo $this->get_field_id('custom_post_id'); ?>" name="<?php echo $this->get_field_name('custom_post_id'); ?>">
  17. <?php query_posts('post_type=content_block&orderby=ID&order=ASC&showposts=-1');
  18. if ( have_posts() ) : while ( have_posts() ) : the_post();
  19. $currentID = get_the_ID();
  20. if($currentID == $custom_post_id)
  21. $extra = 'selected' and
  22. $widgetExtraTitle = get_the_title();
  23. else
  24. $extra = '';
  25. echo '<option value="'.$currentID.'" '.$extra.'>'.get_the_title().'</option>';
  26. endwhile; else:
  27. echo '<option value="empty">' . __('No content blocks available', 'custom-post-widget') . '</option>';
  28. endif; ?>
  29. </select>
  30. </label>
  31. </p>
  32. <input type="hidden" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $widgetExtraTitle; ?>" />
  33. <?php wp_reset_query(); ?>
  34. <p>
  35. <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' ); ?>" />
  36. <label for="<?php echo $this->get_field_id( 'show_custom_post_title' ); ?>"><?php echo __('Show Post Title', 'custom-post-widget') ?></label>
  37. </p> <?php
  38. }
  39.  
  40. function update($new_instance, $old_instance) {
  41. $instance = $old_instance;
  42. $instance['custom_post_id'] = strip_tags($new_instance['custom_post_id']);
  43. $instance['show_custom_post_title'] = $new_instance['show_custom_post_title'];
  44. return $instance;
  45. }
  46.  
  47. function widget($args, $instance) {
  48. extract($args);
  49. $custom_post_id = ( $instance['custom_post_id'] != '' ) ? esc_attr($instance['custom_post_id']) : __('Find', 'custom-post-widget');
  50. // Variables from the widget settings.
  51. $show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : false;
  52. //$content_post = get_post($custom_post_id);
  53. $args = array(
  54. 'post_status'=> 'publish',
  55. 'post_type'=>'content_block',
  56. 'orderby' => 'post_date',
  57. 'order' => 'DESC',
  58. 'numberposts' => 1
  59. );
  60.  
  61. $get_posts = null;
  62. $get_posts = new WP_Query();
  63.  
  64. $get_posts->query($args);
  65. if ( $get_posts->have_posts() ) : while ( $get_posts->have_posts() ) : $get_posts->the_post();
  66.  
  67. echo $before_widget;
  68. if ( $show_custom_post_title ) {
  69. echo $before_title . get_the_title() . $after_title; // This is the line that displays the title (only if show title is set)
  70. }
  71. echo the_content(); // This is where the actual content of the custom post is being displayed
  72.  
  73. echo $after_widget;
  74.  
  75. endwhile; endif;
  76. }
  77.  
  78. }
  79.  
  80. // Create the Content Block custom post type
  81. add_action('init', 'my_content_block_post_type_init');
  82.  
  83. function my_content_block_post_type_init() {
  84. $labels = array(
  85. 'name' => _x('Content Blocks', 'post type general name', 'custom-post-widget'),
  86. 'singular_name' => _x('Content Block', 'post type singular name', 'custom-post-widget'),
  87. 'plural_name' => _x('Content Blocks', 'post type plural name', 'custom-post-widget'),
  88. 'add_new' => _x('Add Content Block', 'block', 'custom-post-widget'),
  89. 'add_new_item' => __('Add New Content Block', 'custom-post-widget'),
  90. 'edit_item' => __('Edit Content Block', 'custom-post-widget'),
  91. 'new_item' => __('New Content Block', 'custom-post-widget'),
  92. 'view_item' => __('View Content Block', 'custom-post-widget'),
  93. 'search_items' => __('Search Content Blocks', 'custom-post-widget'),
  94. 'not_found' => __('No Content Blocks Found', 'custom-post-widget'),
  95. 'not_found_in_trash' => __('No Content Blocks found in Trash', 'custom-post-widget'),
  96. 'parent_item_colon' => ''
  97. );
  98. $options = array(
  99. 'labels' => $labels,
  100. 'public' => false,
  101. 'publicly_queryable' => false,
  102. 'exclude_from_search' => true,
  103. 'show_ui' => true,
  104. 'query_var' => true,
  105. 'rewrite' => true,
  106. 'capability_type' => 'post',
  107. 'hierarchical' => false,
  108. 'menu_position' => null,
  109. 'supports' => array('title','editor','revisions','thumbnail','author')
  110. );
  111. register_post_type('content_block',$options);
  112. }
  113.  
  114. // Add custom styles to admin screen and menu
  115. add_action('admin_head', 'content_block_header');
  116.  
  117. function content_block_header() {
  118. global $post_type; ?>
  119. <style type="text/css">
  120. <!--
  121. <?php if (($post_type == 'content_block')) : ?>
  122. #icon-edit { background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL; ?>images/contentblock-32.png') no-repeat 0 0 !important; }
  123. <?php endif; ?>
  124. #adminmenu #menu-posts-content_block div.wp-menu-image{background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL;?>images/contentblock.png') no-repeat center -32px;}
  125. #adminmenu #menu-posts-content_block:hover div.wp-menu-image,#adminmenu #menu-posts-content_block.wp-has-current-submenu div.wp-menu-image{background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL;?>images/contentblock.png') no-repeat center 0px;}
  126. -->
  127. </style>
  128. <?php
  129. }
  130.  
  131. add_filter('post_updated_messages', 'content_block_messages');
  132.  
  133. function content_block_messages( $messages ) {
  134. $messages['content_block'] = array(
  135. 0 => '',
  136. 1 => sprintf( __('Content Block updated. <a href="%s">View Content Block</a>', 'custom-post-widget'), esc_url( get_permalink(isset($post->ID) ? $post->ID : null) ) ),
  137. 2 => __('Custom field updated.', 'custom-post-widget'),
  138. 3 => __('Custom field deleted.', 'custom-post-widget'),
  139. 4 => __('Content Block updated.', 'custom-post-widget'),
  140. 5 => isset($_GET['revision']) ? sprintf( __('Content Block restored to revision from %s', 'custom-post-widget'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  141. 6 => sprintf( __('Content Block published. <a href="%s">View Content Block</a>', 'custom-post-widget'), esc_url( get_permalink(isset($post->ID) ? $post->ID : null) ) ),
  142. 7 => __('Block saved.', 'custom-post-widget'),
  143. 8 => sprintf( __('Content Block submitted. <a target="_blank" href="%s">Preview Content Block</a>', 'custom-post-widget'), esc_url( add_query_arg( 'preview', 'true', get_permalink(isset($post->ID) ? $post->ID : null) ) ) ),
  144. 9 => sprintf( __('Content Block scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview block</a>', 'custom-post-widget'), date_i18n( __( 'M j, Y @ G:i' , 'custom-post-widget'), strtotime(isset($post->post_date) ? $post->post_date : null) ), esc_url( get_permalink(isset($post->ID) ? $post->ID : null) ) ),
  145. 10 => sprintf( __('Content Block draft updated. <a target="_blank" href="%s">Preview Content Block</a>', 'custom-post-widget'), esc_url( add_query_arg( 'preview', 'true', get_permalink(isset($post->ID) ? $post->ID : null) ) ) ),
  146. );
  147. return $messages;
  148. }
  149.  
  150. // Add the ability to display the content block in a reqular post using a shortcode
  151. function custom_post_widget_shortcode($atts) {
  152. extract(shortcode_atts(array(
  153. 'id' => '',
  154. ), $atts));
  155.  
  156. $content = "";
  157.  
  158. if($id != "") {
  159. $args = array(
  160. 'post__in' => array($id),
  161. 'post_type' => 'content_block',
  162. 'post_status'=>'publish'
  163. );
  164.  
  165. $content_post = get_posts($args);
  166.  
  167. foreach( $content_post as $post ) :
  168. $content .= apply_filters('the_content', $post->post_content);
  169. endforeach;
  170. }
  171.  
  172. return $content;
  173. }
  174.  
  175. add_shortcode('content_block', 'custom_post_widget_shortcode');
  176.  
  177. // Add button above editor
  178. function add_content_block_icon($initcontext) {
  179. return $initcontext.
  180. '<a id="add_content_block" style="text-decoration:none;" class="thickbox" title="' . __("Add Content Block", 'custom-post-widget') . '" href="' . CUSTOM_POST_WIDGET_URL . 'popup.php?type=add_content_block_popup&amp;TB_inline=true&amp;inlineId=content_block_form">
  181. <img onclick="return false;" alt="' . __("Add Content Block", 'custom-post-widget') . '" src="' . CUSTOM_POST_WIDGET_URL . 'images/contentblock-13.png">
  182. </a>';
  183. }
  184.  
  185. add_filter('media_buttons_context', 'add_content_block_icon');
  186.  
  187. require_once( CUSTOM_POST_WIDGET_DIR . '/popup.php' );
  188.  
  189. if(!defined( 'CUSTOM_POST_WIDGET_CURRENT_PAGE' ))
  190. define( 'CUSTOM_POST_WIDGET_CURRENT_PAGE', basename($_SERVER['PHP_SELF']) );
  191. if(in_array(CUSTOM_POST_WIDGET_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))) {
  192. add_action('admin_footer', 'add_content_block_popup');
  193. }
  194. ?>
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement