PageRenderTime 102ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/functions.php

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