as a replacement for in index.php (and other post templates) to display an image for a post's author (inside "the loop") Use as a replacement for in the sidebar section of your site to list all authors for the blog using an image and/or text. PROCESS BY WHICH IMAGE IS LOCATED OR TEXT USED INSTEAD: 1. If a post has an 'author_image' custom field defined, the plugin checks if the value is the valid name of an image in the defined image directory. If not, it appends each of the defined image extensions, in turn, to the custom field's value, then checks if an image can be found. 2. If no image has been found yet, it looks in the image directory for a file with a name that is the author's login name with a file extension matching one of the ones defined in image extensions. 3. If no image has been found yet, it looks in the image directory for a file with a name that is the author's ID with a file extension matching one of the ones defined in image extensions. 4. If an image has been found and the "Show author name as well?" setting is true, or if no author image has been found and "Show author name if image is found?" setting is true, then the author's name is appended/used. NOTES: - The plugins admin options page allows you to control if the author's name should appear alongside the author image or not, if the author name should be shown in the event no author image could be found, the image directory, the support image extensions, and if and what you want the author image to link to. - c2c_the_author_image() is a drop-in replacement for WordPress's the_author() , allowing the author for the post to have an image displayed in lieu of the name (if an image can be found). - Images output by c2c_the_author_image() are defined with class="author_image" for stylesheet manipulation - c2c_wp_list_authors_images() is a drop-in replacement for WordPress's wp_list_authors(), allowing all authors for a blog to be listed with an image (if present) - Images output by c2c_wp_list_authors_images() are defined with class="list_author_image" for stylesheet manipulation TIPS: - You can take advantage of the $image_dir argument to display different author images under different contexts, i.e. locate your images in different directories '/wp-content/images/authors/small/' and '/wp-content/images/authors/large/' and decide on context where to get the image(s) from. Compatible with WordPress 2.2+, 2.3+, and 2.5. =>> Read the accompanying readme.txt file for more information. Also, visit the plugin's homepage =>> for more information and the latest updates Installation: 1. Download the file http://www.coffee2code.com/wp-plugins/author-images.zip and unzip it into your /wp-content/plugins/ directory. 2. Activate the plugin through the 'Plugins' admin menu in WordPress 3. Go to the Options -> Author Images (or in WP 2.5: Setings -> Author Images) admin options page. Optionally customize the options. 4. Create the directory(s) that will contain author image(s) (by default this would be /wp-content/images/authors/) 5. Put images into the specified image directory, using the author login name or the author ID as the filename, with whatever extension you plan to support, i.e. admin.gif or 1.gif 6. Modify your index.php and/or other template files to include calls to and/or . (Read the rest of these notes for more info.) 7. (optional) Add the custom field 'author_image' to posts to explicitly define an image for the author for just that post. Otherwise, the plugin will proceed to attempt to find the author image. 8. (optional) Add the class "author_image" to your CSS file to control the display of the image located by c2c_the_author_image(), and/or add the class "list_author_image" to your CSS file to control the display of the images listed by c2c_wp_list_authors_images(). */ /* Copyright (c) 2005-2008 by Scott Reilly (aka coffee2code) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ if ( !class_exists('AuthorImages') ) : class AuthorImages { var $admin_options_name = 'c2c_author_images'; var $nonce_field = 'update-author_images'; var $show_admin = true; // Change this to false if you don't want the plugin's admin page shown. var $config = array(); var $options = array(); // Don't use this directly function AuthorImages() { $this->config = array( // input can be 'checkbox', 'text', 'textarea', 'inline_textarea', 'select', 'hidden', or 'none' // an input type of 'select' must then have an 'options' value (an array) specified // datatype can be 'array' or 'hash' (the default should be defined as an array()) // can also specify input_attributes 'show_name' => array('input' => 'checkbox', 'default' => true, 'label' => 'Show author name as well?', 'help' => 'Should the author\'s name be shown in addition to the author\'s image?'), 'show_name_if_no_image' => array('input' => 'checkbox', 'default' => true, 'label' => 'Show author name if no image is found?', 'help' => 'Should the author\'s name be shown if the author\'s image can\'t be found?'), 'link_type' => array('input' => 'select', 'default' => 'posts', 'options' => array('posts', 'site', 'none'), 'label' => 'Image linking', 'help' => 'How should the author\'s image be linked?
posts : link to the archive of the author\'s posts
site : link to the author\'s website (if defined for the author)
none : don\'t link the author image'), 'image_extensions' => array('input' => 'text', 'default' => 'png gif jpg', 'label' => 'Supported image extensions', 'help' => 'Space-separated list of possible image extensions for image'), 'image_dir' => array('input' => 'text', 'default' => '/wp-content/images/authors/', 'label' => 'Image directory', 'help' => 'Directory, relative to the root of your site, containing the author images.') ); add_action('admin_menu', array(&$this, 'admin_menu')); } function install() { $this->options = $this->get_options(); update_option($this->admin_options_name, $this->options); } function admin_menu() { if ( $this->show_admin ) add_options_page('Author Images', 'Author Images', 9, basename(__FILE__), array(&$this, 'options_page')); } function get_options() { if ( !empty($this->options)) return $this->options; // Derive options from the config $options = array(); foreach (array_keys($this->config) as $opt) { $options[$opt] = $this->config[$opt]['default']; } $existing_options = get_option($this->admin_options_name); if (!empty($existing_options)) { foreach ($existing_options as $key => $value) $options[$key] = $value; } $this->options = $options; return $options; } function options_page() { $options = $this->get_options(); // See if user has submitted form if ( isset($_POST['submitted']) ) { check_admin_referer($this->nonce_field); foreach (array_keys($options) AS $opt) { $options[$opt] = htmlspecialchars(stripslashes($_POST[$opt])); $input = $this->config[$opt]['input']; if (($input == 'checkbox') && !$options[$opt]) $options[$opt] = 0; if ($this->config[$opt]['datatype'] == 'array') { if ($input == 'text') $options[$opt] = explode(',', str_replace(array(', ', ' ', ','), ',', $options[$opt])); else $options[$opt] = array_map('trim', explode("\n", trim($options[$opt]))); } elseif ($this->config[$opt]['datatype'] == 'hash') { if ( !empty($options[$opt]) ) { $new_values = array(); foreach (explode("\n", $options[$opt]) AS $line) { list($shortcut, $text) = array_map('trim', explode("=>", $line, 2)); if (!empty($shortcut)) $new_values[str_replace('\\', '', $shortcut)] = str_replace('\\', '', $text); } $options[$opt] = $new_values; } } } // Remember to put all the other options into the array or they'll get lost! update_option($this->admin_options_name, $options); echo "

Plugin settings saved.

"; } $action_url = $_SERVER[PHP_SELF] . '?page=' . basename(__FILE__); echo <<

Author Image(s) Plugin Options

Local images/avatars for authors.

END; wp_nonce_field($this->nonce_field); echo ''; foreach (array_keys($options) as $opt) { $input = $this->config[$opt]['input']; if ($input == 'none') continue; $label = $this->config[$opt]['label']; $value = $options[$opt]; if ($input == 'checkbox') { $checked = ($value == 1) ? 'checked=checked ' : ''; $value = 1; } else { $checked = ''; }; if ($this->config[$opt]['datatype'] == 'array') { if (!is_array($value)) $value = ''; else { if ($input == 'textarea' || $input == 'inline_textarea') $value = implode("\n", $value); else $value = implode(', ', $value); } } elseif ($this->config[$opt]['datatype'] == 'hash') { if (!is_array($value)) $value = ''; else { $new_value = ''; foreach ($value AS $shortcut => $replacement) { $new_value .= "$shortcut => $replacement\n"; } $value = $new_value; } } echo ""; if ($input == 'textarea') { echo ""; } echo <<
END; $logo = get_option('siteurl') . '/wp-content/plugins/' . basename($_GET['page'], '.php') . '/c2c_minilogo.png'; echo << #c2c { text-align:center; color:#888; background-color:#ffffef; padding:5px 0 0; margin-top:12px; border-style:solid; border-color:#dadada; border-width:1px 0; } #c2c div { margin:0 auto; padding:5px 40px 0 0; width:45%; min-height:40px; background:url('$logo') no-repeat top right; } #c2c span { display:block; font-size:x-small; } END; echo << .percenttags, .percentfuncs dd { color:#666; } .percenttags li { margin:0.1em 0.1em 0.1em 0.4em; } .percenttags li strong { color:#111; } .percentfuncs { list-style:bullet; } .percentfuncs dd { margin-left:2em; } .percentfuncs dt { font-weight:bold; } .percentfuncs ul, .percentfuncs ul li { padding:0; margin:0; } .percentfuncs ul li { margin-left:25px; } .percentfuncs ul li code { font-family:"Courier New",Courier,mono; font-weight:normal; padding:0pt; } .percentfuncs ul li code pre { width:100%; overflow-x:scroll; }

Template functions

There are two template functions made available by this plugins.

  • function c2c_the_author_image( \$before = '', \$after = '', \$image_dir = '' )
    A drop-in replacement for WordPress's the_author() , allowing the author for the post to have an image displayed in lieu of the name (if an image can be found).
  • function c2c_wp_list_authors_images( \$args = '' )
    A drop-in replacement for WordPress's wp_list_authors(), allowing all authors for a blog to be listed with an image (if present).

Arguments:

\$before
Optional. The text and/or HTML to appear before the author image/text, if any such text would be returned..
Default value: ''
\$after
Optional. The text and/or HTML to appear after the author image/text, if any such text would be returned.
Default value: ''
\$image_dir
Optional. The directory, relative to the root of your blog, in which to find the author images. If not set, it defaults to the value configured via the plugin's admin options page.
Default value: ''
\$args
Optional. An array of configuration options. All but the last two match up with the supported arguments of the wp_list_authors() function.
  • optioncount : default of false;
  • exclude_admin : default of true; should the admin user be excluded from the listing?
  • show_fullname : default of false; should the user's first and last name be shown?
  • hide_empty : default of true; should authors who have not made any posts be excluded from the listings?
  • feed : default of ''
  • feed_image : default of ''
  • echo : default of true; should the listing be echoed to the page?
  • show_name : default of '' which means it'll abide by the plugin's setting value; should the name of the author be shown in addition to the image?
  • show_name_if_no_image : default of '' which means it'll abide by the plugin's setting value; should the author's name be shown if the author doesn't have an image?
END; } function find_author_image( $authorID, $image_dir = '', $check_custom = false ) { $options = $this->get_options(); $image_dir = ($image_dir ? $image_dir : $options['image_dir']); $image_extensions = explode(' ', $options['image_extensions']); $output = ''; // Check to see if user specified image filename to use as author image $custom = $check_custom ? get_post_custom_keys('author_image') : ''; if ( !empty($custom) ) { if ( file_exists(ABSPATH . $image_dir . '/' . $custom) ) $output = get_settings('siteurl') . $image_dir . '/' . $custom; else { foreach ($image_extensions as $image_extension) { if ( file_exists(ABSPATH . $image_dir . '/' . $custom . '.' . $image_extension) ) { $output = get_settings('siteurl') . $image_dir . '/' . $custom . '.' . $image_extension; break; } } } } // If no author image by this point, look for image file based on user login or user id if ( empty($output) ) { foreach ($image_extensions as $image_extension) { $authordata = get_userdata($authorID); if ( file_exists(ABSPATH . $image_dir . '/' . $authordata->user_login . '.' . $image_extension) ) { $output = get_settings('siteurl') . $image_dir . '/' . $authordata->user_login . '.' . $image_extension; break; } elseif ( file_exists(ABSPATH . $image_dir . '/' . $authorID . '.' . $image_extension) ) { $output = get_settings('siteurl') . $image_dir . '/' . $authorID . '.' . $image_extension; break; } } } return $output; } // end function find_author_image() function get_the_author_image( $before = '', $after = '', $image_dir = '' ) { global $authordata; $options = $this->get_options(); // Get the link to the image, if there is one if ( empty($options['image_extensions']) ) $output = ''; else $output = $this->find_author_image($authordata->ID, $image_dir, true); $author_name = wp_specialchars(get_the_author()); if ( $output ) { $output = "$author_name"; if ( $options['show_name'] ) $output .= $author_name; } else { if ( !$options['show_name_if_no_image'] ) return; $output = $author_name; } // Hyperlink the author image/name, if requested to do so if ( 'posts' == $options['link_type'] ) $output = ''.$output.''; elseif ( ('site' == $link_type) && $authordata->user_url ) $output = ''.$output.''; return $before . $output . $after; } // end function get_the_author_image() } // end AuthorImages endif; // end if !class_exists() if ( class_exists('AuthorImages') ) : // Get the ball rolling $author_images = new AuthorImages(); // Actions and filters if (isset($author_images)) { register_activation_hook( __FILE__, array(&$author_images, 'install') ); } endif; function c2c_get_the_author_image( $before = '', $after = '', $image_dir = '' ) { global $author_images; return $author_images->get_the_author_image($before, $after, $image_dir); } function c2c_the_author_image( $before = '', $after = '', $image_dir = '' ) { echo c2c_get_the_author_image($before, $after, $image_dir); } function c2c_wp_list_authors_images( $args = '' ) { global $wpdb, $author_images; $options = $author_images->get_options(); if ( is_array($args) ) $r = &$args; else parse_str($args, $r); $defaults = array( 'optioncount' => false, 'exclude_admin' => true, 'show_fullname' => false, 'hide_empty' => true, 'feed' => '', 'feed_image' => '', 'echo' => true, 'show_name' => '', 'show_name_if_no_image' => ''); $r = wp_parse_args($args, $defaults); extract($r, EXTR_SKIP); $return = ''; $query = "SELECT ID, user_nicename FROM $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name"; $authors = $wpdb->get_results($query); $author_count = array(); foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) { $author_count[$row->post_author] = $row->count; } foreach ( (array) $authors as $author ) { $author = get_userdata($author->ID); $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0; $name = $author->display_name; if ($show_fullname && ($author->first_name != '' && $author->last_name != '')) $name = "$author->first_name $author->last_name"; $name = wp_specialchars($name); if ( ($posts == 0) && $hide_empty ) continue; // First determine what to show (author image or text of author name) $image = $author_images->find_author_image($author->ID); if (! empty($image) ) { $image = "" . $name . ""; if ( $show_name || (($show_name == '') && $options['show_name']) ) $image .= $name; } else { if ( $show_name_if_no_image || (($show_name_if_no_image == '') && $options['show_name_if_no_image']) ) $image = $name; else continue; } // Wrap $image in link, if applicable $link = ''; if ( 'posts' == $options['link_type'] ) if ( $posts != 0 ) $link = 'display_name)).'">'.$image.''; elseif ( ('site' == $link_type) && $author->user_url ) $link = ''.$image.''; if ( empty($link) ) $link = $image; if ( (! empty($feed_image)) || (! empty($feed)) ) { $link .= ' '; if (empty($feed_image)) $link .= '('; $link .= ''; else $link .= $name; $link .= ''; if (empty($feed_image)) $link .= ')'; } if ($optioncount) $link .= ' (' . $posts . ')'; $return = "
  • $link
  • \n"; if ( !$echo ) return $return; echo $return; } } // end function c2c_wp_list_authors_images() ?>
    "; if ($label) echo "$label :
    "; echo "'; } else { echo "
    $label : "; if ($input == "inline_textarea") echo "'; elseif ($input == 'select') { echo ""; } else echo "config[$opt]['input_attributes']} />"; } if ($this->config[$opt]['help']) { echo "
    "; echo $this->config[$opt]['help']; echo ""; } echo "