PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/functions.php

http://half-baked.googlecode.com/
PHP | 320 lines | 182 code | 28 blank | 110 comment | 22 complexity | 897fc8c3ab522b535f65934c1f5f3175 MD5 | raw file
  1. <?php
  2. /**
  3. * Theme Functions
  4. *
  5. * Custom functions, default variable definitions and supported features.
  6. *
  7. * @package Half-Baked
  8. * @author Guy Fisher
  9. * @copyright Copyright Š 2007 Guy M. Fisher
  10. * @license http://gnu.org/licenses/gpl-2.0.html
  11. */
  12. /**
  13. * Sets up default variable definitions and supported features.
  14. *
  15. * @since 1.6.1
  16. *
  17. * @uses add_theme_support()
  18. */
  19. function half_baked_setup_theme() {
  20. global $content_width;
  21. if ( ! isset( $content_width ) ) {
  22. $content_width = 640; // global content width
  23. }
  24. add_theme_support( 'automatic-feed-links' ); // automatic rss feed links
  25. }
  26. add_action( 'after_setup_theme', 'half_baked_setup_theme' );
  27. /**
  28. * Enqueues scripts for output in <head> element.
  29. *
  30. * @since 1.6
  31. *
  32. * @uses wp_enqueue_script()
  33. */
  34. function half_baked_enqueue_scripts() {
  35. $theme = get_theme( get_current_theme() );
  36. wp_enqueue_script( 'fitvids', get_template_directory_uri() . '/scripts/jquery.fitvids.js', array( 'jquery' ), '1.0' ); // fluid videos
  37. wp_enqueue_script( 'accordion', get_template_directory_uri() . '/scripts/accordion.js', array( 'scriptaculous-effects' ) ); // accordion widget
  38. wp_enqueue_script( 'half-baked', get_template_directory_uri() . '/scripts/half-baked.js', array( 'fitvids', 'accordion' ), $theme['Version'] );
  39. }
  40. add_action( 'wp_enqueue_scripts', 'half_baked_enqueue_scripts' );
  41. function half_baked_contact() {
  42. /* Echos a link to any page with a slug matching "contact" ... or a link to the administrator's e-mail address. */
  43. global $wpdb;
  44. $contact_id = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_name = 'contact' AND post_type = 'page' AND post_status = 'publish'");
  45. if (is_null($contact_id)) {
  46. $contact_email = antispambot('mailto:' . get_option('admin_email'));
  47. echo "<p id=\"contact\"><a href=\"$contact_email\" title=\"Send an e-mail message to " . get_bloginfo('name') . '">Contact ' . get_bloginfo('name') . "</a></p>\n";
  48. }
  49. else echo '<p id="contact"><a href="' . get_permalink($contact_id) . '">Contact ' . get_bloginfo('name') . "</a></p>\n";
  50. }
  51. function half_baked_about() {
  52. /* Echos a link to any page with a slug matching "about" ... or a link to the WordPress home page. */
  53. global $wpdb;
  54. $about_id = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_name = 'about' AND post_type = 'page' AND post_status = 'publish'");
  55. if ($about_id) {
  56. echo '<p id="about"><a href="' . get_permalink($about_id) . '">About ' . get_bloginfo('name') . "</a></p>\n";
  57. }
  58. else echo "<p id=\"about\" class=\"wplink\"><a href=\"http://wordpress.org/\">Powered by WordPress</a></p>\n";
  59. }
  60. /**
  61. * Displays subcategories of current category.
  62. *
  63. * Echoes a comma-separated list of links to subcategories of current category in main index template.
  64. *
  65. * @uses get_categories()
  66. * @return string|null Echoes subcategories or returns null
  67. */
  68. function half_baked_subcategories() {
  69. $subcategories = get_categories( array( 'parent' => get_query_var( 'cat' ) ) );
  70. if ( empty( $subcategories ) ) {
  71. return;
  72. }
  73. echo '<p>Subcategories: ';
  74. foreach ( $subcategories as $key => $value ) {
  75. if ( $key > 0 ) {
  76. if ( $key == count( $subcategories ) - 1 ) {
  77. echo ' and ';
  78. }
  79. else echo ', ';
  80. }
  81. echo '<a href="' . esc_url( get_category_link( $value->term_id ) ) . '" title="' . esc_attr( $value->description ) . '">' . $value->name . '</a>';
  82. }
  83. echo '</p>';
  84. }
  85. /**
  86. * Removes brackets from ellipses at end of excerpts.
  87. *
  88. * Filters the excerpt more string with the excerpt_more filter hook.
  89. *
  90. * @see wp_trim_excerpt()
  91. * @since 1.6.1
  92. *
  93. * @param string $more Excerpt more string
  94. * @return string Excerpt more string replaced by unbracketed ellipsis
  95. */
  96. function half_baked_excerpt_more( $more ) {
  97. return str_replace( '[...]', '&#8230;', $more );
  98. }
  99. add_filter( 'excerpt_more', 'half_baked_excerpt_more' );
  100. /**
  101. * Replaces last comma in categories list with "and" separator.
  102. *
  103. * Filters the categories list with the_category filter hook.
  104. *
  105. * @see get_the_category_list()
  106. * @since 1.6.1
  107. *
  108. * @param string $thelist Categories list
  109. * @return string Categories list with last comma replaced by "and"
  110. */
  111. function half_baked_category( $thelist ) {
  112. $last_comma = strrpos( $thelist, ', ' );
  113. if ( $last_comma === false ) {
  114. return $thelist;
  115. }
  116. else return substr_replace( $thelist, ' and ', $last_comma, 2 );
  117. }
  118. add_filter( 'the_category', 'half_baked_category' );
  119. /**
  120. * Replaces last comma in tags list with "and" separator.
  121. *
  122. * Filters the tags list on single posts with the_tags filter hook.
  123. *
  124. * @see get_the_tag_list()
  125. * @since 1.6.1
  126. *
  127. * @param string $term_list Tags list
  128. * @return string Tags list with last comma replaced by "and"
  129. */
  130. function half_baked_tags( $term_list ) {
  131. $last_comma = strrpos( $term_list, ', ' );
  132. if ( ! is_single() || $last_comma === false ) {
  133. return $term_list;
  134. }
  135. else return substr_replace( $term_list, ' and ', $last_comma, 2 );
  136. }
  137. add_filter( 'the_tags', 'half_baked_tags' );
  138. /**
  139. * Formats comments and pingbacks in comments loop.
  140. *
  141. * Callback function for wp_list_comments in comments template.
  142. *
  143. * @see Walker_Comment::start_el()
  144. * @since 1.6
  145. *
  146. * @param object $comment Current comment
  147. * @param array $args Formatting options
  148. * @param int $depth Depth of comment in reference to parents
  149. */
  150. function half_baked_start_el( $comment, $args, $depth ) {
  151. $GLOBALS['comment'] = $comment;
  152. ?>
  153. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
  154. <?php if ( 'comment' == get_comment_type() ) : ?>
  155. <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  156. <h4>Comment by <?php comment_author(); ?></h4>
  157. <div class="comment-author vcard">
  158. <div class="avatar"><?php echo( get_avatar( $comment, 32 ) ); ?></div>
  159. <div class="dateline"><?php comment_date(); if ( $comment->comment_author_url ) comment_author_url_link( '', ' | ', '&nbsp;&raquo;' ); ?></div>
  160. </div>
  161. <?php if ( '0' == $comment->comment_approved ) { ?>
  162. <p><em>Your comment is awaiting moderation.</em></p>
  163. <?php } ?>
  164. <?php comment_text(); ?>
  165. <div class="bookmarks">
  166. <img class="icon" src="<?php echo( get_template_directory_uri() ); ?>/images/twotone/bookmark.gif" width="16" height="16" alt="" />&nbsp;<a href="#comment-<?php comment_ID(); ?>" title="Permanent link to this comment">Bookmark</a>
  167. <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'], 'before' => '&nbsp;&nbsp;<img class="icon" src="' . get_template_directory_uri() . '/images/twotone/quote.gif" width="16" height="16" alt="" />&nbsp;' ) ) ); ?>
  168. <?php edit_comment_link( 'Edit', '&nbsp;&nbsp;<img class="icon" src="' . get_template_directory_uri() . '/images/twotone/edit.gif" width="16" height="16" alt="" />&nbsp;' ); ?>
  169. </div>
  170. </div>
  171. <?php else : ?>
  172. <img class="icon" src="<?php echo( get_template_directory_uri() ); ?>/images/twotone/back-forth.gif" width="16" height="16" alt="Pingback" />&nbsp;<?php comment_author_link(); ?>
  173. <?php
  174. endif;
  175. }
  176. /**
  177. * Enqueues comment reply script.
  178. *
  179. * Enqueues comment reply script if comment form is displayed and threaded comments are enabled.
  180. *
  181. * @link http://wpengineer.com/2358/enqueue-comment-reply-js-the-right-way/ WP Engineer
  182. * @since 1.6
  183. *
  184. * @uses wp_enqueue_script()
  185. */
  186. function half_baked_enqueue_comment_reply() {
  187. if ( get_option( 'thread_comments' ) ) {
  188. wp_enqueue_script( 'comment-reply' );
  189. }
  190. }
  191. add_action( 'comment_form_before', 'half_baked_enqueue_comment_reply' );
  192. /**
  193. * Filters comment form fields and strings.
  194. *
  195. * Inserts <em> element in comment notes and removes allowed tags note.
  196. *
  197. * @since 1.6
  198. *
  199. * @param array $defaults Comment form fields and strings
  200. * @return array Filtered form fields and strings
  201. */
  202. function half_baked_comment_form_defaults( $defaults ) {
  203. $defaults['comment_notes_before'] = str_replace( array( '<p class="comment-notes">', '</p>' ), array( '<p class="comment-notes"><em>', '</em></p>' ), $defaults['comment_notes_before'] );
  204. $defaults['comment_notes_after'] = '';
  205. return $defaults;
  206. }
  207. add_filter( 'comment_form_defaults', 'half_baked_comment_form_defaults' );
  208. /**
  209. * Inserts <fieldset> opening tag before comment form fields.
  210. *
  211. * @since 1.6
  212. */
  213. function half_baked_comment_form_before_fields() {
  214. echo '<fieldset class="comment-form-fields">';
  215. }
  216. add_action( 'comment_form_before_fields', 'half_baked_comment_form_before_fields' );
  217. /**
  218. * Inserts <fieldset> closing tag after comment form fields.
  219. *
  220. * @since 1.6
  221. */
  222. function half_baked_comment_form_after_fields() {
  223. echo '</fieldset>';
  224. }
  225. add_action( 'comment_form_after_fields', 'half_baked_comment_form_after_fields' );
  226. /* Sidebar Widgets */
  227. if ( function_exists( 'register_sidebar' ) ) {
  228. register_sidebar(
  229. array(
  230. 'name' => 'Main Sidebar',
  231. 'description' => 'Widgets displayed in the main sidebar',
  232. 'before_widget' => "\t" . '<div id="%1$s" class="widget %2$s">' . "\n",
  233. 'after_widget' => "\n\t</div>\n",
  234. 'before_title' => "\t\t<h3>",
  235. 'after_title' => "</h3>\n"
  236. )
  237. );
  238. register_sidebar(
  239. array(
  240. 'name' => 'Accordion',
  241. 'description' => 'Widgets displayed inside the Half-Baked Accordion widget',
  242. 'before_widget' => "\t\t\t" . '<div id="%1$s" class="widget %2$s">' . "\n",
  243. 'after_widget' => "\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\t\t\t</div>\n",
  244. 'before_title' => "\t\t\t\t<h3>",
  245. 'after_title' => "</h3>\n\t\t\t\t<div class=\"widget_content\">\n\t\t\t\t\t<div class=\"scriptaculous\">\n"
  246. )
  247. );
  248. }
  249. function half_baked_widgets_ini() {
  250. /* Initializes the custom widgets built into the Half-Baked theme. */
  251. if (!function_exists('register_sidebar_widget')) {
  252. return;
  253. }
  254. function widget_half_baked_search($args) { // Search Widget
  255. extract($args);
  256. echo $before_widget;
  257. echo $before_title . 'Search' . $after_title;
  258. get_search_form();
  259. echo $after_widget;
  260. }
  261. $widget_ops = array('classname' => 'widget_search', 'description' => 'A search form for the Half-Baked theme');
  262. wp_register_sidebar_widget('search', 'Search', 'widget_half_baked_search', $widget_ops);
  263. function widget_half_baked_accordion($args) { // Accordion Widget
  264. if (is_home()) {
  265. extract($args);
  266. echo $before_widget;
  267. echo "\t\t<div class=\"accordion_content\">\n";
  268. dynamic_sidebar('Accordion');
  269. echo "\t\t</div>\n";
  270. echo "\t</div>\n";
  271. }
  272. }
  273. $widget_ops = array('classname' => 'widget_half_baked_accordion', 'description' => 'A Scriptaculous accordion for the Half-Baked theme. Drag this widget to the Main Sidebar to display the accordion and then drag the widgets you want displayed inside the accordion to the Accordion sidebar.');
  274. wp_register_sidebar_widget('half-baked-accordion', 'Half-Baked Accordion', 'widget_half_baked_accordion', $widget_ops);
  275. function widget_half_baked_meta($args) { // Meta Widget
  276. extract($args);
  277. echo $before_widget;
  278. echo $before_title . 'Meta' . $after_title;
  279. include TEMPLATEPATH . '/meta.php';
  280. echo $after_widget;
  281. }
  282. $widget_ops = array('classname' => 'widget_meta', 'description' => 'Meta information and login link for the Half-Baked theme');
  283. wp_register_sidebar_widget('meta', 'Meta', 'widget_half_baked_meta', $widget_ops);
  284. }
  285. add_action('widgets_init', 'half_baked_widgets_ini');
  286. ?>