PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/twentyten/functions.php

https://bitbucket.org/abnopanda/wordpress
PHP | 516 lines | 258 code | 39 blank | 219 comment | 17 complexity | 183a403bc5261213efd9e62f8b992b08 MD5 | raw file
  1. <?php
  2. /**
  3. * TwentyTen functions and definitions
  4. *
  5. * Sets up the theme and provides some helper functions. Some helper functions
  6. * are used in the theme as custom template tags. Others are attached to action and
  7. * filter hooks in WordPress to change core functionality.
  8. *
  9. * The first function, twentyten_setup(), sets up the theme by registering support
  10. * for various features in WordPress, such as post thumbnails, navigation menus, and the like.
  11. *
  12. * When using a child theme (see http://codex.wordpress.org/Theme_Development and
  13. * http://codex.wordpress.org/Child_Themes), you can override certain functions
  14. * (those wrapped in a function_exists() call) by defining them first in your child theme's
  15. * functions.php file. The child theme's functions.php file is included before the parent
  16. * theme's file, so the child theme functions would be used.
  17. *
  18. * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
  19. * to a filter or action hook. The hook can be removed by using remove_action() or
  20. * remove_filter() and you can attach your own function to the hook.
  21. *
  22. * We can remove the parent theme's hook only after it is attached, which means we need to
  23. * wait until setting up the child theme:
  24. *
  25. * <code>
  26. * add_action( 'after_setup_theme', 'my_child_theme_setup' );
  27. * function my_child_theme_setup() {
  28. * // We are providing our own filter for excerpt_length (or using the unfiltered value)
  29. * remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );
  30. * ...
  31. * }
  32. * </code>
  33. *
  34. * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
  35. *
  36. * @package WordPress
  37. * @subpackage Twenty_Ten
  38. * @since Twenty Ten 1.0
  39. */
  40. /**
  41. * Set the content width based on the theme's design and stylesheet.
  42. *
  43. * Used to set the width of images and content. Should be equal to the width the theme
  44. * is designed for, generally via the style.css stylesheet.
  45. */
  46. if ( ! isset( $content_width ) )
  47. $content_width = 640;
  48. /** Tell WordPress to run twentyten_setup() when the 'after_setup_theme' hook is run. */
  49. add_action( 'after_setup_theme', 'twentyten_setup' );
  50. if ( ! function_exists( 'twentyten_setup' ) ):
  51. /**
  52. * Sets up theme defaults and registers support for various WordPress features.
  53. *
  54. * Note that this function is hooked into the after_setup_theme hook, which runs
  55. * before the init hook. The init hook is too late for some features, such as indicating
  56. * support post thumbnails.
  57. *
  58. * To override twentyten_setup() in a child theme, add your own twentyten_setup to your child theme's
  59. * functions.php file.
  60. *
  61. * @uses add_theme_support() To add support for post thumbnails, custom headers and backgrounds, and automatic feed links.
  62. * @uses register_nav_menus() To add support for navigation menus.
  63. * @uses add_editor_style() To style the visual editor.
  64. * @uses load_theme_textdomain() For translation/localization support.
  65. * @uses register_default_headers() To register the default custom header images provided with the theme.
  66. * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
  67. *
  68. * @since Twenty Ten 1.0
  69. */
  70. function twentyten_setup() {
  71. // This theme styles the visual editor with editor-style.css to match the theme style.
  72. add_editor_style();
  73. // Post Format support. You can also use the legacy "gallery" or "asides" (note the plural) categories.
  74. add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
  75. // This theme uses post thumbnails
  76. add_theme_support( 'post-thumbnails' );
  77. // Add default posts and comments RSS feed links to head
  78. add_theme_support( 'automatic-feed-links' );
  79. // Make theme available for translation
  80. // Translations can be filed in the /languages/ directory
  81. load_theme_textdomain( 'twentyten', get_template_directory() . '/languages' );
  82. // This theme uses wp_nav_menu() in one location.
  83. register_nav_menus( array(
  84. 'primary' => __( 'Primary Navigation', 'twentyten' ),
  85. ) );
  86. // This theme allows users to set a custom background.
  87. add_theme_support( 'custom-background', array(
  88. // Let WordPress know what our default background color is.
  89. 'default-color' => 'f1f1f1',
  90. ) );
  91. // The custom header business starts here.
  92. $custom_header_support = array(
  93. // The default image to use.
  94. // The %s is a placeholder for the theme template directory URI.
  95. 'default-image' => '%s/images/headers/path.jpg',
  96. // The height and width of our custom header.
  97. 'width' => apply_filters( 'twentyten_header_image_width', 940 ),
  98. 'height' => apply_filters( 'twentyten_header_image_height', 198 ),
  99. // Support flexible heights.
  100. 'flex-height' => true,
  101. // Don't support text inside the header image.
  102. 'header-text' => false,
  103. // Callback for styling the header preview in the admin.
  104. 'admin-head-callback' => 'twentyten_admin_header_style',
  105. );
  106. add_theme_support( 'custom-header', $custom_header_support );
  107. if ( ! function_exists( 'get_custom_header' ) ) {
  108. // This is all for compatibility with versions of WordPress prior to 3.4.
  109. define( 'HEADER_TEXTCOLOR', '' );
  110. define( 'NO_HEADER_TEXT', true );
  111. define( 'HEADER_IMAGE', $custom_header_support['default-image'] );
  112. define( 'HEADER_IMAGE_WIDTH', $custom_header_support['width'] );
  113. define( 'HEADER_IMAGE_HEIGHT', $custom_header_support['height'] );
  114. add_custom_image_header( '', $custom_header_support['admin-head-callback'] );
  115. add_custom_background();
  116. }
  117. // We'll be using post thumbnails for custom header images on posts and pages.
  118. // We want them to be 940 pixels wide by 198 pixels tall.
  119. // Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
  120. set_post_thumbnail_size( $custom_header_support['width'], $custom_header_support['height'], true );
  121. // ... and thus ends the custom header business.
  122. // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
  123. register_default_headers( array(
  124. 'berries' => array(
  125. 'url' => '%s/images/headers/berries.jpg',
  126. 'thumbnail_url' => '%s/images/headers/berries-thumbnail.jpg',
  127. /* translators: header image description */
  128. 'description' => __( 'Berries', 'twentyten' )
  129. ),
  130. 'cherryblossom' => array(
  131. 'url' => '%s/images/headers/cherryblossoms.jpg',
  132. 'thumbnail_url' => '%s/images/headers/cherryblossoms-thumbnail.jpg',
  133. /* translators: header image description */
  134. 'description' => __( 'Cherry Blossoms', 'twentyten' )
  135. ),
  136. 'concave' => array(
  137. 'url' => '%s/images/headers/concave.jpg',
  138. 'thumbnail_url' => '%s/images/headers/concave-thumbnail.jpg',
  139. /* translators: header image description */
  140. 'description' => __( 'Concave', 'twentyten' )
  141. ),
  142. 'fern' => array(
  143. 'url' => '%s/images/headers/fern.jpg',
  144. 'thumbnail_url' => '%s/images/headers/fern-thumbnail.jpg',
  145. /* translators: header image description */
  146. 'description' => __( 'Fern', 'twentyten' )
  147. ),
  148. 'forestfloor' => array(
  149. 'url' => '%s/images/headers/forestfloor.jpg',
  150. 'thumbnail_url' => '%s/images/headers/forestfloor-thumbnail.jpg',
  151. /* translators: header image description */
  152. 'description' => __( 'Forest Floor', 'twentyten' )
  153. ),
  154. 'inkwell' => array(
  155. 'url' => '%s/images/headers/inkwell.jpg',
  156. 'thumbnail_url' => '%s/images/headers/inkwell-thumbnail.jpg',
  157. /* translators: header image description */
  158. 'description' => __( 'Inkwell', 'twentyten' )
  159. ),
  160. 'path' => array(
  161. 'url' => '%s/images/headers/path.jpg',
  162. 'thumbnail_url' => '%s/images/headers/path-thumbnail.jpg',
  163. /* translators: header image description */
  164. 'description' => __( 'Path', 'twentyten' )
  165. ),
  166. 'sunset' => array(
  167. 'url' => '%s/images/headers/sunset.jpg',
  168. 'thumbnail_url' => '%s/images/headers/sunset-thumbnail.jpg',
  169. /* translators: header image description */
  170. 'description' => __( 'Sunset', 'twentyten' )
  171. )
  172. ) );
  173. }
  174. endif;
  175. if ( ! function_exists( 'twentyten_admin_header_style' ) ) :
  176. /**
  177. * Styles the header image displayed on the Appearance > Header admin panel.
  178. *
  179. * Referenced via add_custom_image_header() in twentyten_setup().
  180. *
  181. * @since Twenty Ten 1.0
  182. */
  183. function twentyten_admin_header_style() {
  184. ?>
  185. <style type="text/css">
  186. /* Shows the same border as on front end */
  187. #headimg {
  188. border-bottom: 1px solid #000;
  189. border-top: 4px solid #000;
  190. }
  191. /* If header-text was supported, you would style the text with these selectors:
  192. #headimg #name { }
  193. #headimg #desc { }
  194. */
  195. </style>
  196. <?php
  197. }
  198. endif;
  199. /**
  200. * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
  201. *
  202. * To override this in a child theme, remove the filter and optionally add
  203. * your own function tied to the wp_page_menu_args filter hook.
  204. *
  205. * @since Twenty Ten 1.0
  206. */
  207. function twentyten_page_menu_args( $args ) {
  208. if ( ! isset( $args['show_home'] ) )
  209. $args['show_home'] = true;
  210. return $args;
  211. }
  212. add_filter( 'wp_page_menu_args', 'twentyten_page_menu_args' );
  213. /**
  214. * Sets the post excerpt length to 40 characters.
  215. *
  216. * To override this length in a child theme, remove the filter and add your own
  217. * function tied to the excerpt_length filter hook.
  218. *
  219. * @since Twenty Ten 1.0
  220. * @return int
  221. */
  222. function twentyten_excerpt_length( $length ) {
  223. return 40;
  224. }
  225. add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
  226. if ( ! function_exists( 'twentyten_continue_reading_link' ) ) :
  227. /**
  228. * Returns a "Continue Reading" link for excerpts
  229. *
  230. * @since Twenty Ten 1.0
  231. * @return string "Continue Reading" link
  232. */
  233. function twentyten_continue_reading_link() {
  234. return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) . '</a>';
  235. }
  236. endif;
  237. /**
  238. * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyten_continue_reading_link().
  239. *
  240. * To override this in a child theme, remove the filter and add your own
  241. * function tied to the excerpt_more filter hook.
  242. *
  243. * @since Twenty Ten 1.0
  244. * @return string An ellipsis
  245. */
  246. function twentyten_auto_excerpt_more( $more ) {
  247. return ' &hellip;' . twentyten_continue_reading_link();
  248. }
  249. add_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
  250. /**
  251. * Adds a pretty "Continue Reading" link to custom post excerpts.
  252. *
  253. * To override this link in a child theme, remove the filter and add your own
  254. * function tied to the get_the_excerpt filter hook.
  255. *
  256. * @since Twenty Ten 1.0
  257. * @return string Excerpt with a pretty "Continue Reading" link
  258. */
  259. function twentyten_custom_excerpt_more( $output ) {
  260. if ( has_excerpt() && ! is_attachment() ) {
  261. $output .= twentyten_continue_reading_link();
  262. }
  263. return $output;
  264. }
  265. add_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );
  266. /**
  267. * Remove inline styles printed when the gallery shortcode is used.
  268. *
  269. * Galleries are styled by the theme in Twenty Ten's style.css. This is just
  270. * a simple filter call that tells WordPress to not use the default styles.
  271. *
  272. * @since Twenty Ten 1.2
  273. */
  274. add_filter( 'use_default_gallery_style', '__return_false' );
  275. /**
  276. * Deprecated way to remove inline styles printed when the gallery shortcode is used.
  277. *
  278. * This function is no longer needed or used. Use the use_default_gallery_style
  279. * filter instead, as seen above.
  280. *
  281. * @since Twenty Ten 1.0
  282. * @deprecated Deprecated in Twenty Ten 1.2 for WordPress 3.1
  283. *
  284. * @return string The gallery style filter, with the styles themselves removed.
  285. */
  286. function twentyten_remove_gallery_css( $css ) {
  287. return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
  288. }
  289. // Backwards compatibility with WordPress 3.0.
  290. if ( version_compare( $GLOBALS['wp_version'], '3.1', '<' ) )
  291. add_filter( 'gallery_style', 'twentyten_remove_gallery_css' );
  292. if ( ! function_exists( 'twentyten_comment' ) ) :
  293. /**
  294. * Template for comments and pingbacks.
  295. *
  296. * To override this walker in a child theme without modifying the comments template
  297. * simply create your own twentyten_comment(), and that function will be used instead.
  298. *
  299. * Used as a callback by wp_list_comments() for displaying the comments.
  300. *
  301. * @since Twenty Ten 1.0
  302. */
  303. function twentyten_comment( $comment, $args, $depth ) {
  304. $GLOBALS['comment'] = $comment;
  305. switch ( $comment->comment_type ) :
  306. case '' :
  307. ?>
  308. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  309. <div id="comment-<?php comment_ID(); ?>">
  310. <div class="comment-author vcard">
  311. <?php echo get_avatar( $comment, 40 ); ?>
  312. <?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
  313. </div><!-- .comment-author .vcard -->
  314. <?php if ( $comment->comment_approved == '0' ) : ?>
  315. <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em>
  316. <br />
  317. <?php endif; ?>
  318. <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
  319. <?php
  320. /* translators: 1: date, 2: time */
  321. printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' );
  322. ?>
  323. </div><!-- .comment-meta .commentmetadata -->
  324. <div class="comment-body"><?php comment_text(); ?></div>
  325. <div class="reply">
  326. <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  327. </div><!-- .reply -->
  328. </div><!-- #comment-## -->
  329. <?php
  330. break;
  331. case 'pingback' :
  332. case 'trackback' :
  333. ?>
  334. <li class="post pingback">
  335. <p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' ); ?></p>
  336. <?php
  337. break;
  338. endswitch;
  339. }
  340. endif;
  341. /**
  342. * Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
  343. *
  344. * To override twentyten_widgets_init() in a child theme, remove the action hook and add your own
  345. * function tied to the init hook.
  346. *
  347. * @since Twenty Ten 1.0
  348. * @uses register_sidebar
  349. */
  350. function twentyten_widgets_init() {
  351. // Area 1, located at the top of the sidebar.
  352. register_sidebar( array(
  353. 'name' => __( 'Primary Widget Area', 'twentyten' ),
  354. 'id' => 'primary-widget-area',
  355. 'description' => __( 'The primary widget area', 'twentyten' ),
  356. 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  357. 'after_widget' => '</li>',
  358. 'before_title' => '<h3 class="widget-title">',
  359. 'after_title' => '</h3>',
  360. ) );
  361. // Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
  362. register_sidebar( array(
  363. 'name' => __( 'Secondary Widget Area', 'twentyten' ),
  364. 'id' => 'secondary-widget-area',
  365. 'description' => __( 'The secondary widget area', 'twentyten' ),
  366. 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  367. 'after_widget' => '</li>',
  368. 'before_title' => '<h3 class="widget-title">',
  369. 'after_title' => '</h3>',
  370. ) );
  371. // Area 3, located in the footer. Empty by default.
  372. register_sidebar( array(
  373. 'name' => __( 'First Footer Widget Area', 'twentyten' ),
  374. 'id' => 'first-footer-widget-area',
  375. 'description' => __( 'The first footer widget area', 'twentyten' ),
  376. 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  377. 'after_widget' => '</li>',
  378. 'before_title' => '<h3 class="widget-title">',
  379. 'after_title' => '</h3>',
  380. ) );
  381. // Area 4, located in the footer. Empty by default.
  382. register_sidebar( array(
  383. 'name' => __( 'Second Footer Widget Area', 'twentyten' ),
  384. 'id' => 'second-footer-widget-area',
  385. 'description' => __( 'The second footer widget area', 'twentyten' ),
  386. 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  387. 'after_widget' => '</li>',
  388. 'before_title' => '<h3 class="widget-title">',
  389. 'after_title' => '</h3>',
  390. ) );
  391. // Area 5, located in the footer. Empty by default.
  392. register_sidebar( array(
  393. 'name' => __( 'Third Footer Widget Area', 'twentyten' ),
  394. 'id' => 'third-footer-widget-area',
  395. 'description' => __( 'The third footer widget area', 'twentyten' ),
  396. 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  397. 'after_widget' => '</li>',
  398. 'before_title' => '<h3 class="widget-title">',
  399. 'after_title' => '</h3>',
  400. ) );
  401. // Area 6, located in the footer. Empty by default.
  402. register_sidebar( array(
  403. 'name' => __( 'Fourth Footer Widget Area', 'twentyten' ),
  404. 'id' => 'fourth-footer-widget-area',
  405. 'description' => __( 'The fourth footer widget area', 'twentyten' ),
  406. 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  407. 'after_widget' => '</li>',
  408. 'before_title' => '<h3 class="widget-title">',
  409. 'after_title' => '</h3>',
  410. ) );
  411. }
  412. /** Register sidebars by running twentyten_widgets_init() on the widgets_init hook. */
  413. add_action( 'widgets_init', 'twentyten_widgets_init' );
  414. /**
  415. * Removes the default styles that are packaged with the Recent Comments widget.
  416. *
  417. * To override this in a child theme, remove the filter and optionally add your own
  418. * function tied to the widgets_init action hook.
  419. *
  420. * This function uses a filter (show_recent_comments_widget_style) new in WordPress 3.1
  421. * to remove the default style. Using Twenty Ten 1.2 in WordPress 3.0 will show the styles,
  422. * but they won't have any effect on the widget in default Twenty Ten styling.
  423. *
  424. * @since Twenty Ten 1.0
  425. */
  426. function twentyten_remove_recent_comments_style() {
  427. add_filter( 'show_recent_comments_widget_style', '__return_false' );
  428. }
  429. add_action( 'widgets_init', 'twentyten_remove_recent_comments_style' );
  430. if ( ! function_exists( 'twentyten_posted_on' ) ) :
  431. /**
  432. * Prints HTML with meta information for the current post-date/time and author.
  433. *
  434. * @since Twenty Ten 1.0
  435. */
  436. function twentyten_posted_on() {
  437. printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
  438. 'meta-prep meta-prep-author',
  439. sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
  440. get_permalink(),
  441. esc_attr( get_the_time() ),
  442. get_the_date()
  443. ),
  444. sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
  445. get_author_posts_url( get_the_author_meta( 'ID' ) ),
  446. esc_attr( sprintf( __( 'View all posts by %s', 'twentyten' ), get_the_author() ) ),
  447. get_the_author()
  448. )
  449. );
  450. }
  451. endif;
  452. if ( ! function_exists( 'twentyten_posted_in' ) ) :
  453. /**
  454. * Prints HTML with meta information for the current post (category, tags and permalink).
  455. *
  456. * @since Twenty Ten 1.0
  457. */
  458. function twentyten_posted_in() {
  459. // Retrieves tag list of current post, separated by commas.
  460. $tag_list = get_the_tag_list( '', ', ' );
  461. if ( $tag_list ) {
  462. $posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
  463. } elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
  464. $posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
  465. } else {
  466. $posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
  467. }
  468. // Prints the string, replacing the placeholders.
  469. printf(
  470. $posted_in,
  471. get_the_category_list( ', ' ),
  472. $tag_list,
  473. get_permalink(),
  474. the_title_attribute( 'echo=0' )
  475. );
  476. }
  477. endif;