Source code for /home/gipetto1/top-frog.com/public_html/script_src/files/my-featured-posts.php

  1.  <?php
  2.  /*
  3.  Plugin Name: My Featured Posts
  4.  Plugin URI: http://denver.wordcamp.org
  5.  Description: Simple plugin to mark posts as featured. Extra comments added after the WordCamp presentation.
  6.  Version: 1
  7.  Author: Shawn Parker
  8.  Author URI: http://denver.wordcamp.org
  9.  */
  10.  
  11.  /**
  12.   * This is a simple plugin written to illustrate the basics of how to develop plugins for WordPress.
  13.   * It has no warranty and no intention of merchantability. It is simply here as an example of how content can
  14.   * be manipulated in WordPress without modifying WordPress or the current Theme files.
  15.   *
  16.   * The code in this example should be followed from top to bottom as a procedure for adding a post meta box to
  17.   * the WordPress admin, then showing the featured status of the post, then adding a widget to show a list
  18.   * of recent featured posts, then showing that same list at the bottom of each post.
  19.   *
  20.   * This document is long because it is (hopefully) verbosely commented to describe what each step is doing.
  21.   *
  22.   * A couple of things to note before we begin.
  23.   * - all the examples below use an mfp_ prefix. This is an easy way to separate your functions from other
  24.   * functions and avoid name space conflicts in the event that another plugin choses the same name as yours.
  25.   * mfp_admin_init is my safer than say my_admin_init
  26.   * - The header sections (the first few lines at the top of this file) are required for WordPress to recognize
  27.   * a php file as being a plugin.
  28.   * - While it isn't necessary to comment your plugins as much as this one has been, it is prudent to document
  29.   * your plugin so much that someone who has never seen it before can come in and figure out what is going on.
  30.   * It may seem frivilous but when you come back to your plugin after 6 months of working on other things you
  31.   * will almost be like someone who has never seen the file before... ;)
  32.   * - If your plugin takes in content that is actually written by users then you'll want to "scrub" that data
  33.   * before shoving it in your database. Basically: never trust user content! Always sanitize it to reduce the
  34.   * chance of malicious things happening on your site.
  35.   */
  36.  
  37.  // ADMIN ACTIONS
  38.   // tie a function in at admin init so we can add a post meta box
  39.   add_action('admin_init','mfp_admin_init');
  40.  
  41.   /**
  42.   * add a post meta-box to the post-edit admin page
  43.   * also add our hook at save post so that we can attach our data as post-meta
  44.   */
  45.   function mfp_admin_init() {
  46.   // tell WordPress the box id, box title, function to run to show the box, the page to display
  47.   // the box on, where to display the box, and what priority to assign to the box display
  48.   add_meta_box('featured-post','FEATURED POST', 'mfp_meta_box','post','side','default');
  49.   // hook on the the save_post action so that we can save our data at the same time the post is saved by WordPress
  50.   add_action('save_post','mfp_save_post');
  51.   }
  52.  
  53.   /**
  54.   * Display a select element in our meta-box
  55.   * I Chose a select element because its easier to only work on its returned data when we find the element in $_POST.
  56.   * This helps the data to be left alone if the post is later edited by an external client that doesn't get the
  57.   * added benefit of seeing our select item.
  58.   *
  59.   * Actions are an opportunity to interject something in the page load sequence. You can do whatever you
  60.   * need to do in an action and the action does not need to return any data.
  61.   *
  62.   * @param object $post - the current post object
  63.   * @param object $box - the current meta-box details
  64.   */
  65.   function mfp_meta_box($post,$box) {
  66.   // pull any existing post_meta data associated with this post, the true statement tells WordPress you're
  67.   // only interested in the value, and to not return anything but the meta value. Default is to return the meta-key as well
  68.   // @NOTE $post is an object which means its member variables are addressed differently than array data by using the -> operator.
  69.   // Check out http://us3.php.net/manual/en/language.types.object.php for an introduction to PHP objects.
  70.   $featured = get_post_meta($post->ID,'_mfp_is_featured',true);
  71.  
  72.   // display our meta-bax
  73.   echo '
  74.   <p class="meta_options">
  75.   <label for="mfp_featured_post"><select name="mfp_featured_post" id="mfp_featured_post">
  76.   <option value="0" '.(is_null($featured) || $featured == '0' ? 'selected="selected" ' : ''). // *
  77.   '>Not Featured</option>
  78.   <option value="1" '.($featured == '1' ? 'selected="selected" ' : ''). // *
  79.   '>Feature this post</option>
  80.   </select>
  81.   </p>
  82.   ';
  83.   // * notation used here is called a ternary operator which basically written out is: if_this ? then_this : else_this
  84.   // go to: http://us2.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary for more information
  85.   }
  86.  
  87.   /**
  88.   * Save post handler
  89.   * Here we check for the presence of our $_POST content and save the appropriate data
  90.   *
  91.   * @param int $post_id - the id of the current post
  92.   * @param object $post - the current post object
  93.   */
  94.   function mfp_save_post($post_id,$post) {
  95.   // save post runs for active posts and for their revisions
  96.   // we don't want to run on revisions so simply return if we're on a post revision
  97.   if($post->post_type == 'revision') { return; }
  98.  
  99.   // only proceed if we find our content in $_POST
  100.   // this way if the post was modified in, say, the iPhone client we don't change the
  101.   // data if the user wasn't presented with the means to do so.
  102.   if(isset($_POST['mfp_featured_post'])) {
  103.   // save the meta key name with a prefixed underscore so that it doesn't
  104.   // show up in the custom post-meta section of the post-edit admin page
  105.   update_post_meta($post_id,'_mfp_is_featured',$_POST['mfp_featured_post']);
  106.   }
  107.   }
  108.  
  109.  // THE TITLE
  110.   // attach to the_title filter so that we can prepend a "featured" flag to it
  111.   add_filter('the_title','mfp_the_title');
  112.  
  113.   /**
  114.   * Prepend a flag to the beginning of the title to mark the post as featured.
  115.   * Filters modify information and then pass the information back out, be sure that after you modify the data
  116.   * that you give it back to WordPress so that your changes show up. Also, if you don't return information at all
  117.   * then the value gets nulled by your function.
  118.   *
  119.   * @param string $the_title - the current title string as selected by WordPress
  120.   * @return string
  121.   */
  122.   function mfp_the_title($the_title) {
  123.   // since we weren't give the post object, grab it from the global
  124.   // scope so that we can pull our information based on its ID
  125.   global $post;
  126.  
  127.   // we only want to operate on titles in the main content area
  128.   // to work everywhere simply remove the in_the_loop() check
  129.   if(in_the_loop()) {
  130.   // get and check the current value of the post-meta, output as necessary
  131.   $featured = get_post_meta($post->ID,'_mfp_is_featured',true);
  132.   if($featured == '1') {
  133.   // prepend our flag to the beginning of the title
  134.   $the_title = '<span class="mfp-featured">[FEATURED POST] </span>'.$the_title;
  135.   }
  136.   }
  137.   // pass the data back to WordPress
  138.   return $the_title;
  139.   }
  140.  
  141.  // WIDGET
  142.   // hook in to widgets_init so that we can add our own widget
  143.   add_action('widgets_init','mfp_widgets_init');
  144.  
  145.   /**
  146.   * Register a sidebar widget. This adds a widget box to WordPress' Widget admin screen
  147.   */
  148.   function mfp_widgets_init() {
  149.   // give WordPress the widget id, the widget title, and the function to use when displaying the widget
  150.   wp_register_sidebar_widget('mfp-widget','My Featured Post', 'mfp_widget');
  151.   }
  152.  
  153.   /**
  154.   * Function to display the widget
  155.   *
  156.   * @param string $args
  157.   * @return void
  158.   */
  159.   function mfp_widget($args) {
  160.   // extract the WordPress defined args in to the local scope
  161.   extract($args);
  162.  
  163.   // The bare minimums of displaying a widget
  164.   // Use standard WordPress args for layout formatting included inside the
  165.   // variables before_widget, after_widget, before_title, after_title.
  166.   // These can be modified globally by using the 'dynamic_sidebar_params' filter.
  167.   echo $before_widget.
  168.   $before_title.'My Featured Posts'.$after_title.
  169.   mfp_featured_posts_list(5). // pull our list from a reusable function that builds a list of featured posts
  170.   $after_widget;
  171.   }
  172.  
  173.   /**
  174.   * Get a list of featured posts
  175.   * Starts a new WP_Query object to query for just the posts we want.
  176.   * Only returns an array of the returned posts, not the whole object since we don't need
  177.   * the entire object for our purposes.
  178.   *
  179.   * @param int $showposts - number of posts to return
  180.   * @return array
  181.   */
  182.   function mfp_get_featured_posts($showposts=5) {
  183.   // most documentation shows the WP_Query object being modified with a string, but it can also
  184.   // take arrays whice are much easier to construct and read in code
  185.   $queryObject = new WP_Query(array(
  186.   'showposts' => $showposts, // how many posts to retrieve
  187.   'meta_key' => '_mfp_is_featured', // only pull the posts with out metadata
  188.   'meta_value' => '1' // only pull the posts who have 1 as a value to our metadata
  189.   ));
  190.   // @NOTE if you plan to distribute your plugin this would be a good opportunity to add filter of your own
  191.   // to the returned content to allow others to modify your plugin's behavior without actually having to modify your plugin
  192.   return $queryObject->posts;
  193.   }
  194.  
  195.   /**
  196.   * Build a list of featured posts
  197.   * Created as an individual function so that we can easily use this elsewhere as either direct output
  198.   * in a template or by another filter (as illustrated later on in this code)
  199.   *
  200.   * @param int $showposts - number of posts to put in the list
  201.   * @param string $id - id to give the list that we're building for easy css targeting
  202.   * @return void
  203.   */
  204.   function mfp_featured_posts_list($showposts,$id='mfp-featured-posts-list') {
  205.   // grab our list of featured posts
  206.   $featured_posts = mfp_get_featured_posts($showposts);
  207.  
  208.   // start out dynamic list output
  209.   $html = '<ul id="'.$id.'">';
  210.  
  211.   // only build the list if we have data to work on, just a security measure to keep PHP from barfing
  212.   // warnings in to your logs, and possibly your screen, when the plugin is turned on and no featured post data is present
  213.   if(count($featured_posts)) {
  214.   foreach($featured_posts as $fp) {
  215.   // use get_permalink to allow WordPress to apply permalink settings, and apply any the_title filters to your title output
  216.   // applying filters ensures that any other filters that need to be run, possibly even your own, are consitently applied across the page load
  217.   // each post is an object so we use the -> operator again to get at the post information
  218.   $html .= '<li><a href="'.get_permalink($fp->ID).'">'.apply_filters('the_title',$fp->post_title).'</a></li>';
  219.   // @NOTE the .= notation used above is an easy way to tack information on to the end of a string
  220.   }
  221.   }
  222.   else {
  223.   // give a nice message if there is nothing to display
  224.   $html .= '<li>There are no featured posts at this time.</li>';
  225.   }
  226.   $html .= '</ul>';
  227.  
  228.   // return the list to who asked for it
  229.   return $html;
  230.   }
  231.  
  232.  
  233.  // THE CONTENT
  234.   // attach to the_content filter so that we can add a list of featured posts at the bottom of every post
  235.   add_filter('the_content','mfp_the_content');
  236.  
  237.   /**
  238.   * Append a list of featured posts to the content, but only on Single post pages
  239.   *
  240.   * @param string $the_content
  241.   * @return void
  242.   */
  243.   function mfp_the_content($the_content) {
  244.   // only operate on single post pages
  245.   if(is_single()) {
  246.   // add the list to the end of the content
  247.   $the_content .= '<div id="more-featured-posts">
  248.   <h2>Other Featured Posts</h2>
  249.   '.mfp_featured_posts_list(10,'more-featured-posts-list'). // pull our list from a reusable function that builds a list of featured posts
  250.   '
  251.   </div>
  252.   ';
  253.   }
  254.   // send the data back to WordPress
  255.   return $the_content;
  256.   }
  257.  ?>