PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/functions.php

https://github.com/ciaranmahoney/bootstrapwp-Twitter-Bootstrap-for-WordPress
PHP | 518 lines | 319 code | 72 blank | 127 comment | 63 complexity | 8e6a805c3371cebf8f889552da4c04d8 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Bootstrap 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. *
  10. * @package WordPress
  11. * @subpackage WP-Bootstrap
  12. * @since WP-Bootstrap 0.1
  13. *
  14. * Last Updated: September 9, 2012
  15. */
  16. if (!defined('BOOTSTRAPWP_VERSION'))
  17. define('BOOTSTRAPWP_VERSION', '.90');
  18. /**
  19. * Declaring the content width based on the theme's design and stylesheet.
  20. */
  21. if ( ! isset( $content_width ) )
  22. $content_width = 770; /* pixels */
  23. /**
  24. * Declaring the theme language domain
  25. */
  26. load_theme_textdomain('bootstrapwp');
  27. /*
  28. | -------------------------------------------------------------------
  29. | Setup Theme
  30. | -------------------------------------------------------------------
  31. |
  32. | */
  33. add_action( 'after_setup_theme', 'bootstrapwp_theme_setup' );
  34. if ( ! function_exists( 'bootstrapwp_theme_setup' ) ):
  35. function bootstrapwp_theme_setup() {
  36. add_theme_support( 'automatic-feed-links' );
  37. /**
  38. * Adds custom menu with wp_page_menu fallback
  39. */
  40. register_nav_menus( array(
  41. 'main-menu' => __( 'Main Menu', 'bootstrapwp' ),
  42. ) );
  43. add_theme_support( 'post-formats', array( 'aside', 'image', 'gallery', 'link', 'quote', 'status', 'video', 'audio', 'chat' ) );
  44. }
  45. endif;
  46. ################################################################################
  47. // Loading All CSS Stylesheets
  48. ################################################################################
  49. function bootstrapwp_css_loader() {
  50. wp_enqueue_style('bootstrapwp', get_template_directory_uri().'/css/bootstrapwp.css', false ,'0.90', 'all' );
  51. wp_enqueue_style('prettify', get_template_directory_uri().'/js/google-code-prettify/prettify.css', false ,'1.0', 'all' );
  52. }
  53. add_action('wp_enqueue_scripts', 'bootstrapwp_css_loader');
  54. ################################################################################
  55. // Loading all JS Script Files. Remove any files you are not using!
  56. ################################################################################
  57. function bootstrapwp_js_loader() {
  58. wp_enqueue_script('bootstrapjs', get_template_directory_uri().'/js/bootstrap.min.js', array('jquery'),'0.90', true );
  59. wp_enqueue_script('prettifyjs', get_template_directory_uri().'/js/google-code-prettify/prettify.js', array('jquery'),'1.0', true );
  60. wp_enqueue_script('demojs', get_template_directory_uri().'/js/bootstrapwp.demo.js', array('jquery'),'0.90', true );
  61. }
  62. add_action('wp_enqueue_scripts', 'bootstrapwp_js_loader');
  63. /*
  64. | -------------------------------------------------------------------
  65. | Top Navigation Bar Customization
  66. | -------------------------------------------------------------------
  67. /**
  68. * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
  69. */
  70. function bootstrapwp_page_menu_args( $args ) {
  71. $args['show_home'] = true;
  72. return $args;
  73. }
  74. add_filter( 'wp_page_menu_args', 'bootstrapwp_page_menu_args' );
  75. /**
  76. * Get file 'includes/class-bootstrap_walker_nav_menu.php' with Custom Walker class methods
  77. * */
  78. include 'includes/class-bootstrapwp_walker_nav_menu.php';
  79. /*
  80. | -------------------------------------------------------------------
  81. | Registering Widget Sections
  82. | -------------------------------------------------------------------
  83. | */
  84. function bootstrapwp_widgets_init() {
  85. register_sidebar( array(
  86. 'name' => 'Page Sidebar',
  87. 'id' => 'sidebar-page',
  88. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  89. 'after_widget' => "</div>",
  90. 'before_title' => '<h4 class="widget-title">',
  91. 'after_title' => '</h4>',
  92. ) );
  93. register_sidebar( array(
  94. 'name' => 'Posts Sidebar',
  95. 'id' => 'sidebar-posts',
  96. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  97. 'after_widget' => "</div>",
  98. 'before_title' => '<h4 class="widget-title">',
  99. 'after_title' => '</h4>',
  100. ) );
  101. register_sidebar(array(
  102. 'name' => 'Home Left',
  103. 'id' => 'home-left',
  104. 'description' => 'Left textbox on homepage',
  105. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  106. 'after_widget' => '</div>',
  107. 'before_title' => '<h2>',
  108. 'after_title' => '</h2>'
  109. ));
  110. register_sidebar(array(
  111. 'name' => 'Home Middle',
  112. 'id' => 'home-middle',
  113. 'description' => 'Middle textbox on homepage',
  114. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  115. 'after_widget' => '</div>',
  116. 'before_title' => '<h2>',
  117. 'after_title' => '</h2>'
  118. ));
  119. register_sidebar(array(
  120. 'name' => 'Home Right',
  121. 'id' => 'home-right',
  122. 'description' => 'Right textbox on homepage',
  123. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  124. 'after_widget' => '</div>',
  125. 'before_title' => '<h2>',
  126. 'after_title' => '</h2>'
  127. ));
  128. register_sidebar(array(
  129. 'name' => 'Footer Content',
  130. 'id' => 'footer-content',
  131. 'description' => 'Footer text or acknowledgements',
  132. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  133. 'after_widget' => '</div>',
  134. 'before_title' => '<h4>',
  135. 'after_title' => '</h4>'
  136. ));
  137. }
  138. add_action( 'init', 'bootstrapwp_widgets_init' );
  139. /*
  140. | -------------------------------------------------------------------
  141. | Adding Post Thumbnails and Image Sizes
  142. | -------------------------------------------------------------------
  143. | */
  144. if ( function_exists( 'add_theme_support' ) ) {
  145. add_theme_support( 'post-thumbnails' );
  146. set_post_thumbnail_size( 160, 120 ); // 160 pixels wide by 120 pixels high
  147. }
  148. if ( function_exists( 'add_image_size' ) ) {
  149. add_image_size( 'bootstrap-small', 260, 180 ); // 260 pixels wide by 180 pixels high
  150. add_image_size( 'bootstrap-medium', 360, 268 ); // 360 pixels wide by 268 pixels high
  151. }
  152. /*
  153. | -------------------------------------------------------------------
  154. | Revising Default Excerpt
  155. | -------------------------------------------------------------------
  156. | Adding filter to post excerpts to contain ...Continue Reading link
  157. | */
  158. function bootstrapwp_excerpt($more) {
  159. global $post;
  160. return '&nbsp; &nbsp;<a href="'. get_permalink($post->ID) . '">...Continue Reading</a>';
  161. }
  162. add_filter('excerpt_more', 'bootstrapwp_excerpt');
  163. if ( ! function_exists( 'bootstrapwp_content_nav' ) ):
  164. /**
  165. * Display navigation to next/previous pages when applicable
  166. */
  167. function bootstrapwp_content_nav( $nav_id ) {
  168. global $wp_query;
  169. ?>
  170. <?php if ( is_single() ) : // navigation links for single posts ?>
  171. <ul class="pager">
  172. <?php previous_post_link( '<li class="previous">%link</li>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'bootstrapwp' ) . '</span> %title' ); ?>
  173. <?php next_post_link( '<li class="next">%link</li>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'bootstrapwp' ) . '</span>' ); ?>
  174. </ul>
  175. <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
  176. <ul class="pager">
  177. <?php if ( get_next_posts_link() ) : ?>
  178. <li class="next"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'bootstrapwp' ) ); ?></li>
  179. <?php endif; ?>
  180. <?php if ( get_previous_posts_link() ) : ?>
  181. <li class="previous"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'bootstrapwp' ) ); ?></li>
  182. <?php endif; ?>
  183. </ul>
  184. <?php endif; ?>
  185. <?php
  186. }
  187. endif; // bootstrapwp_content_nav
  188. if ( ! function_exists( 'bootstrapwp_comment' ) ) :
  189. /**
  190. * Template for comments and pingbacks.
  191. *
  192. * To override this walker in a child theme without modifying the comments template
  193. * simply create your own bootstrap_comment(), and that function will be used instead.
  194. *
  195. * Used as a callback by wp_list_comments() for displaying the comments.
  196. *
  197. * @since WP-Bootstrap .5
  198. */
  199. function bootstrapwp_comment( $comment, $args, $depth ) {
  200. $GLOBALS['comment'] = $comment;
  201. switch ( $comment->comment_type ) :
  202. case 'pingback' :
  203. case 'trackback' :
  204. ?>
  205. <li class="post pingback">
  206. <p><?php _e( 'Pingback:', 'bootstrap' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'bootstrap' ), ' ' ); ?></p>
  207. <?php
  208. break;
  209. default :
  210. ?>
  211. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  212. <article id="comment-<?php comment_ID(); ?>" class="comment">
  213. <footer>
  214. <div class="comment-author vcard">
  215. <?php echo get_avatar( $comment, 40 ); ?>
  216. <?php printf( __( '%s <span class="says">says:</span>', 'bootstrap' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
  217. </div><!-- .comment-author .vcard -->
  218. <?php if ( $comment->comment_approved == '0' ) : ?>
  219. <em><?php _e( 'Your comment is awaiting moderation.', 'bootstrap' ); ?></em>
  220. <br />
  221. <?php endif; ?>
  222. <div class="comment-meta commentmetadata">
  223. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><time pubdate datetime="<?php comment_time( 'c' ); ?>">
  224. <?php
  225. /* translators: 1: date, 2: time */
  226. printf( __( '%1$s at %2$s', 'bootstrap' ), get_comment_date(), get_comment_time() ); ?>
  227. </time></a>
  228. <?php edit_comment_link( __( '(Edit)', 'bootstrap' ), ' ' );
  229. ?>
  230. </div><!-- .comment-meta .commentmetadata -->
  231. </footer>
  232. <div class="comment-content"><?php comment_text(); ?></div>
  233. <div class="reply">
  234. <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  235. </div><!-- .reply -->
  236. </article><!-- #comment-## -->
  237. <?php
  238. break;
  239. endswitch;
  240. }
  241. endif; // ends check for bootstrapwp_comment()
  242. if ( ! function_exists( 'bootstrapwp_posted_on' ) ) :
  243. /**
  244. * Prints HTML with meta information for the current post-date/time and author.
  245. * Create your own bootstrap_posted_on to override in a child theme
  246. *
  247. * @since WP-Bootstrap .5
  248. */
  249. function bootstrapwp_posted_on() {
  250. printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="byline"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'bootstrap' ),
  251. esc_url( get_permalink() ),
  252. esc_attr( get_the_time() ),
  253. esc_attr( get_the_date( 'c' ) ),
  254. esc_html( get_the_date() ),
  255. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  256. esc_attr( sprintf( __( 'View all posts by %s', 'bootstrap' ), get_the_author() ) ),
  257. esc_html( get_the_author() )
  258. );
  259. }
  260. endif;
  261. /**
  262. * Adds custom classes to the array of body classes.
  263. *
  264. * @since WP-Bootstrap .5
  265. */
  266. function bootstrapwp_body_classes( $classes ) {
  267. // Adds a class of single-author to blogs with only 1 published author
  268. if ( ! is_multi_author() ) {
  269. $classes[] = 'single-author';
  270. }
  271. return $classes;
  272. }
  273. add_filter( 'body_class', 'bootstrapwp_body_classes' );
  274. /**
  275. * Returns true if a blog has more than 1 category
  276. *
  277. * @since WP-Bootstrap .5
  278. */
  279. function bootstrapwp_categorized_blog() {
  280. if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
  281. // Create an array of all the categories that are attached to posts
  282. $all_the_cool_cats = get_categories( array(
  283. 'hide_empty' => 1,
  284. ) );
  285. // Count the number of categories that are attached to the posts
  286. $all_the_cool_cats = count( $all_the_cool_cats );
  287. set_transient( 'all_the_cool_cats', $all_the_cool_cats );
  288. }
  289. if ( '1' != $all_the_cool_cats ) {
  290. // This blog has more than 1 category so bootstrap_categorized_blog should return true
  291. return true;
  292. } else {
  293. // This blog has only 1 category so bootstrap_categorized_blog should return false
  294. return false;
  295. }
  296. }
  297. /**
  298. * Flush out the transients used in bootstrapwp_categorized_blog
  299. *
  300. * @since bootstrap 1.2
  301. */
  302. function bootstrapwp_category_transient_flusher() {
  303. // Like, beat it. Dig?
  304. delete_transient( 'all_the_cool_cats' );
  305. }
  306. add_action( 'edit_category', 'bootstrapwp_category_transient_flusher' );
  307. add_action( 'save_post', 'bootstrapwp_category_transient_flusher' );
  308. /**
  309. * Filter in a link to a content ID attribute for the next/previous image links on image attachment pages
  310. */
  311. function bootstrapwp_enhanced_image_navigation( $url ) {
  312. global $post;
  313. if ( wp_attachment_is_image( $post->ID ) )
  314. $url = $url . '#main';
  315. return $url;
  316. }
  317. add_filter( 'attachment_link', 'bootstrapwp_enhanced_image_navigation' );
  318. /*
  319. | -------------------------------------------------------------------
  320. | Checking for Post Thumbnail
  321. | -------------------------------------------------------------------
  322. |
  323. | */
  324. function bootstrapwp_post_thumbnail_check() {
  325. global $post;
  326. if (get_the_post_thumbnail()) {
  327. return true; }
  328. else { return false; }
  329. }
  330. /*
  331. | -------------------------------------------------------------------
  332. | Setting Featured Image (Post Thumbnail)
  333. | -------------------------------------------------------------------
  334. | Will automatically add the first image attached to a post as the Featured Image if post does not have a featured image previously set.
  335. | */
  336. function bootstrapwp_autoset_featured_img() {
  337. $post_thumbnail = bootstrapwp_post_thumbnail_check();
  338. if ($post_thumbnail == true ){
  339. return the_post_thumbnail();
  340. }
  341. if ($post_thumbnail == false ){
  342. $image_args = array(
  343. 'post_type' => 'attachment',
  344. 'numberposts' => 1,
  345. 'post_mime_type' => 'image',
  346. 'post_parent' => $post->ID,
  347. 'order' => 'desc'
  348. );
  349. $attached_image = get_children( $image_args );
  350. if ($attached_image) {
  351. foreach ($attached_image as $attachment_id => $attachment) {
  352. set_post_thumbnail($post->ID, $attachment_id);
  353. }
  354. return the_post_thumbnail();
  355. } else { return " ";}
  356. }
  357. } //end function
  358. /*
  359. | -------------------------------------------------------------------
  360. | Adding Breadcrumbs
  361. | -------------------------------------------------------------------
  362. |
  363. | */
  364. function bootstrapwp_breadcrumbs() {
  365. $delimiter = '<span class="divider">/</span>';
  366. $home = 'Home'; // text for the 'Home' link
  367. $before = '<li class="active">'; // tag before the current crumb
  368. $after = '</li>'; // tag after the current crumb
  369. if ( !is_home() && !is_front_page() || is_paged() ) {
  370. echo '<ul class="breadcrumb">';
  371. global $post;
  372. $homeLink = home_url();
  373. echo '<li><a href="' . $homeLink . '">' . $home . '</a></li> ' . $delimiter . ' ';
  374. if ( is_category() ) {
  375. global $wp_query;
  376. $cat_obj = $wp_query->get_queried_object();
  377. $thisCat = $cat_obj->term_id;
  378. $thisCat = get_category($thisCat);
  379. $parentCat = get_category($thisCat->parent);
  380. if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
  381. echo $before . 'Archive by category "' . single_cat_title('', false) . '"' . $after;
  382. } elseif ( is_day() ) {
  383. echo '<li><a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a></li> ' . $delimiter . ' ';
  384. echo '<li><a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a></li> ' . $delimiter . ' ';
  385. echo $before . get_the_time('d') . $after;
  386. } elseif ( is_month() ) {
  387. echo '<li><a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a></li> ' . $delimiter . ' ';
  388. echo $before . get_the_time('F') . $after;
  389. } elseif ( is_year() ) {
  390. echo $before . get_the_time('Y') . $after;
  391. } elseif ( is_single() && !is_attachment() ) {
  392. if ( get_post_type() != 'post' ) {
  393. $post_type = get_post_type_object(get_post_type());
  394. $slug = $post_type->rewrite;
  395. echo '<li><a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a></li> ' . $delimiter . ' ';
  396. echo $before . get_the_title() . $after;
  397. } else {
  398. $cat = get_the_category(); $cat = $cat[0];
  399. echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  400. echo $before . get_the_title() . $after;
  401. }
  402. } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  403. $post_type = get_post_type_object(get_post_type());
  404. echo $before . $post_type->labels->singular_name . $after;
  405. } elseif ( is_attachment() ) {
  406. $parent = get_post($post->post_parent);
  407. $cat = get_the_category($parent->ID); $cat = $cat[0];
  408. echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  409. echo '<li><a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a></li> ' . $delimiter . ' ';
  410. echo $before . get_the_title() . $after;
  411. } elseif ( is_page() && !$post->post_parent ) {
  412. echo $before . get_the_title() . $after;
  413. } elseif ( is_page() && $post->post_parent ) {
  414. $parent_id = $post->post_parent;
  415. $breadcrumbs = array();
  416. while ($parent_id) {
  417. $page = get_page($parent_id);
  418. $breadcrumbs[] = '<li><a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a></li>';
  419. $parent_id = $page->post_parent;
  420. }
  421. $breadcrumbs = array_reverse($breadcrumbs);
  422. foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
  423. echo $before . get_the_title() . $after;
  424. } elseif ( is_search() ) {
  425. echo $before . 'Search results for "' . get_search_query() . '"' . $after;
  426. } elseif ( is_tag() ) {
  427. echo $before . 'Posts tagged "' . single_tag_title('', false) . '"' . $after;
  428. } elseif ( is_author() ) {
  429. global $author;
  430. $userdata = get_userdata($author);
  431. echo $before . 'Articles posted by ' . $userdata->display_name . $after;
  432. } elseif ( is_404() ) {
  433. echo $before . 'Error 404' . $after;
  434. }
  435. if ( get_query_var('paged') ) {
  436. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  437. echo __('Page', 'bootstrapwp') . ' ' . get_query_var('paged');
  438. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  439. }
  440. echo '</ul>';
  441. }
  442. } // end bootstrapwp_breadcrumbs()
  443. /**
  444. * This theme was built with PHP, Semantic HTML, CSS, love, and a bootstrap.
  445. */