PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/t/PrettyCreative/functions.php

https://bitbucket.org/matthewselby/wpdev
PHP | 459 lines | 278 code | 78 blank | 103 comment | 18 complexity | 30a2b4187ee8b2d2ec3179aff24623a5 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, LGPL-3.0, LGPL-2.1, AGPL-1.0, BSD-3-Clause, MIT, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. //* Start the engine
  3. include_once( get_template_directory() . '/lib/init.php' );
  4. //* Child theme (do not remove)
  5. define( 'CHILD_THEME_NAME', 'Pretty Creative Theme' );
  6. define( 'CHILD_THEME_URL', 'http://my.studiopress.com/themes/pretty-creative/' );
  7. define( 'CHILD_THEME_VERSION', '2.1.3' );
  8. //* Enqueue Google Fonts
  9. add_action( 'wp_enqueue_scripts', 'genesis_sample_google_fonts' );
  10. function genesis_sample_google_fonts() {
  11. wp_enqueue_style( 'dashicons' );
  12. wp_enqueue_style( 'google-fonts', '//fonts.googleapis.com/css?family=Open+Sans:400,300', array(), CHILD_THEME_VERSION );
  13. wp_enqueue_script( 'sticky-nav', get_bloginfo( 'stylesheet_directory' ) . '/js/sticky-nav.js', array( 'jquery' ), '', true );
  14. wp_enqueue_script( 'prettycreative-responsive-menu', get_stylesheet_directory_uri() . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0', true );
  15. }
  16. //* Add HTML5 markup structure
  17. add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ) );
  18. //* Add viewport meta tag for mobile browsers
  19. add_theme_support( 'genesis-responsive-viewport' );
  20. //* Add Accent color to customizer
  21. require_once( get_stylesheet_directory() . '/lib/customize.php' );
  22. //* Add support for custom header
  23. add_theme_support( 'custom-header', array(
  24. 'width' => 1200,
  25. 'height' => 400,
  26. 'header-selector' => '.site-title a',
  27. 'header-text' => false,
  28. ) );
  29. //* Add support for 2-column footer widgets
  30. add_theme_support( 'genesis-footer-widgets', 2);
  31. //* Register Image Sizes
  32. add_image_size( 'portfolio', 800, 800, true );
  33. add_image_size( 'featuredposts', 600, 350, true );
  34. add_image_size( 'sidebar-featured', 360, 250, true );
  35. add_image_size( 'sidebar-featured', 800, 533, true );
  36. add_image_size( 'home-page-featured', 800, 800, true );
  37. //* Remove comment form allowed tags
  38. add_filter( 'comment_form_defaults', 'prettycreative_remove_comment_form_allowed_tags' );
  39. function prettycreative_remove_comment_form_allowed_tags( $defaults ) {
  40. $defaults['comment_notes_after'] = '';
  41. return $defaults;
  42. }
  43. /* Add support for after entry widget */
  44. add_theme_support( 'genesis-after-entry-widget-area' );
  45. //* Footer credits
  46. add_filter('genesis_footer_creds_text', 'prettycreative_footer_creds_filter');
  47. function prettycreative_footer_creds_filter( $creds ) {
  48. $creds = '[footer_copyright] &middot; Pretty Creative WordPress Theme by, <a href="http://prettydarncute.com">Pretty Darn Cute Design</a>';
  49. return $creds;
  50. }
  51. //* Modify the size of the Gravatar in the entry comments
  52. add_filter( 'genesis_comment_list_args', 'prettycreative_comments_gravatar' );
  53. function prettycreative_comments_gravatar( $args ) {
  54. $args['avatar_size'] = 120;
  55. return $args;
  56. }
  57. //* Modify the size of the Gravatar in the author box
  58. add_filter( 'genesis_author_box_gravatar_size', 'prettycreative_author_box_gravatar' );
  59. function prettycreative_author_box_gravatar( $size ) {
  60. return 120;
  61. }
  62. //* Reposition the primary navigation menu
  63. remove_action( 'genesis_after_header', 'genesis_do_nav' );
  64. add_action( 'genesis_before_header', 'genesis_do_nav' );
  65. //* Add Support for Woo Commerce
  66. add_theme_support( 'genesis-connect-woocommerce' );
  67. add_action( 'after_setup_theme', 'woocommerce_support' );
  68. function woocommerce_support() {
  69. add_theme_support( 'woocommerce' );
  70. }
  71. //* Customize the entry meta
  72. add_filter( 'genesis_post_info', 'prettycreative_post_info_filter' );
  73. function prettycreative_post_info_filter($post_info) {
  74. $post_info = '[post_date] by [post_author_posts_link] [post_comments] [post_edit]';
  75. return $post_info;
  76. }
  77. //* Add Author Box Widget
  78. add_filter( 'genesis_author_box', 'prettycreative_author_box', 10, 6 );
  79. function prettycreative_author_box( $output, $context, $pattern, $gravatar, $title, $description ) {
  80. if ( is_active_sidebar( 'author-box-custom' ) ) {
  81. ob_start();
  82. genesis_widget_area( 'author-box-custom', array( 'before' => '<div class="author-box-custom">', 'after' => '</div>', ) );
  83. $description .= ob_get_contents();
  84. ob_end_clean();
  85. }
  86. $output = sprintf( $pattern, $gravatar, $title, $description );
  87. return $output;
  88. }
  89. //* Add Support for Comment Numbering
  90. add_action ( 'genesis_before_comment', 'prettycreative_numbered_comments' );
  91. function prettycreative_numbered_comments () {
  92. if (function_exists( 'gtcn_comment_numbering' ))
  93. echo gtcn_comment_numbering($comment->comment_ID, $args);
  94. }
  95. //* Modify the length of post excerpts
  96. add_filter( 'excerpt_length', 'prettycreative_excerpt_length' );
  97. function prettycreative_excerpt_length( $length ) {
  98. return 70;
  99. }
  100. //* Genesis Previous/Next Post Post Navigation
  101. add_action( 'genesis_before_comments', 'prettycreative_prev_next_post_nav' );
  102. function prettycreative_prev_next_post_nav() {
  103. if ( is_single() ) {
  104. echo '<div class="prev-next-navigation">';
  105. previous_post_link( '<div class="previous">%link</div>', '%title' );
  106. next_post_link( '<div class="next">%link</div>', '%title' );
  107. echo '</div><!-- .prev-next-navigation -->';
  108. }
  109. }
  110. //* Add Read More Link for Custom Excerpts
  111. function excerpt_read_more_link($output) {
  112. global $post;
  113. return $output . '<a href="'. get_permalink($post->ID) . '"> <div class="readmorelink"><div class="rmtext">[ Read More ]</div></div></a>';
  114. }
  115. add_filter('the_excerpt', 'excerpt_read_more_link');
  116. add_filter('excerpt_more','__return_false');
  117. //* Reposition the secondary navigation menu
  118. remove_action( 'genesis_after_header', 'genesis_do_subnav' );
  119. add_action( 'genesis_before_footer', 'genesis_do_subnav' );
  120. //* Reduce the secondary navigation menu to one level depth
  121. add_filter( 'wp_nav_menu_args', 'prettycreative_secondary_menu_args' );
  122. function prettycreative_secondary_menu_args( $args ){
  123. if( 'secondary' != $args['theme_location'] )
  124. return $args;
  125. $args['depth'] = 1;
  126. return $args;
  127. }
  128. //* Subscribe Widget
  129. add_action( 'genesis_before_header', 'custom_subscribe_widget' );
  130. function custom_subscribe_widget() {
  131. genesis_widget_area( 'subscribewidget', array(
  132. 'before' => '<div class=subscribe-widget widget-area">',
  133. 'after' => '</div>',
  134. ) );
  135. }
  136. //* Remove post meta
  137. remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
  138. //* Woocommerce products per page
  139. add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );
  140. //* Customize search form input box text
  141. add_filter( 'genesis_search_text', 'prettycreative_search_text' );
  142. function prettycreative_search_text( $text ) {
  143. return esc_attr( 'search' );
  144. }
  145. //* Unregister header right widget area
  146. unregister_sidebar( 'header-right' );
  147. //* Setup widget counts
  148. function prettycreative_count_widgets( $id ) {
  149. global $sidebars_widgets;
  150. if ( isset( $sidebars_widgets[ $id ] ) ) {
  151. return count( $sidebars_widgets[ $id ] );
  152. }
  153. }
  154. function prettycreative_widget_area_class( $id ) {
  155. $count = prettycreative_count_widgets( $id );
  156. $class = '';
  157. if( $count == 1 || $count < 9 ) {
  158. $classes = array(
  159. 'zero',
  160. 'one',
  161. 'two',
  162. 'three',
  163. 'four',
  164. 'five',
  165. 'six',
  166. 'seven',
  167. 'eight',
  168. );
  169. $class = $classes[ $count ] . '-widget';
  170. $class = $count == 1 ? $class : $class . 's';
  171. return $class;
  172. } else {
  173. $class = 'widget-thirds';
  174. return $class;
  175. }
  176. }
  177. //* Navigation Search and Social Icons
  178. add_filter( 'genesis_search_button_text', 'prettycreative_search_button_text' );
  179. function prettycreative_search_button_text( $text ) {
  180. return esc_attr( '&#xf179;' );
  181. }
  182. add_filter( 'genesis_nav_items', 'prettycreative_social_icons', 10, 2 );
  183. add_filter( 'wp_nav_menu_items', 'prettycreative_social_icons', 10, 2 );
  184. function prettycreative_social_icons($menu, $args) {
  185. $args = (array)$args;
  186. if ( 'primary' !== $args['theme_location'] )
  187. return $menu;
  188. ob_start();
  189. genesis_widget_area('nav-widget-area');
  190. $social = ob_get_clean();
  191. return $menu . $social;
  192. }
  193. /**
  194. * Default Category Title
  195. *
  196. * @author Bill Erickson
  197. * @url http://www.billerickson.net/default-category-and-tag-titles
  198. *
  199. * @param string $headline
  200. * @param object $term
  201. * @return string $headline
  202. */
  203. function be_default_category_title( $headline, $term ) {
  204. if( ( is_category() || is_tag() || is_tax() ) && empty( $headline ) )
  205. $headline = $term->name;
  206. return $headline;
  207. }
  208. add_filter( 'genesis_term_meta_headline', 'be_default_category_title', 10, 2 );
  209. /**
  210. * Image sanitization callback example.
  211. *
  212. * Checks the image's file extension and mime type against a whitelist. If they're allowed,
  213. * send back the filename, otherwise, return the setting default.
  214. *
  215. * - Sanitization: image file extension
  216. * - Control: text, WP_Customize_Image_Control
  217. *
  218. * @see wp_check_filetype() https://developer.wordpress.org/reference/functions/wp_check_filetype/
  219. *
  220. * @param string $image Image filename.
  221. * @param WP_Customize_Setting $setting Setting instance.
  222. * @return string The image filename if the extension is allowed; otherwise, the setting default.
  223. */
  224. function theme_slug_sanitize_image( $image, $setting ) {
  225. /*
  226. * Array of valid image file types.
  227. *
  228. * The array includes image mime types that are included in wp_get_mime_types()
  229. */
  230. $mimes = array(
  231. 'jpg|jpeg|jpe' => 'image/jpeg',
  232. 'gif' => 'image/gif',
  233. 'png' => 'image/png',
  234. 'bmp' => 'image/bmp',
  235. 'tif|tiff' => 'image/tiff',
  236. 'ico' => 'image/x-icon'
  237. );
  238. // Return an array with file extension and mime_type.
  239. $file = wp_check_filetype( $image, $mimes );
  240. // If $image has a valid mime_type, return it; otherwise, return the default.
  241. return ( $file['ext'] ? $image : $setting->default );
  242. }
  243. /**
  244. * Theme Options Customizer Implementation.
  245. *
  246. * @link http://ottopress.com/2012/how-to-leverage-the-theme-customizer-in-your-own-themes/
  247. *
  248. * @param WP_Customize_Manager $wp_customize Object that holds the customizer data.
  249. */
  250. function pc_register_theme_customizer( $wp_customize ){
  251. /*
  252. * Failsafe is safe
  253. */
  254. if ( ! isset( $wp_customize ) ) {
  255. return;
  256. }
  257. /**
  258. * Add 'Home Top' Section.
  259. */
  260. $wp_customize->add_section(
  261. // $id
  262. 'pc_section_home_top',
  263. // $args
  264. array(
  265. 'title' => __( 'Home Top', 'theme-slug' ),
  266. // 'description' => __( 'Some description for the options in the Home Top section', 'theme-slug' ),
  267. )
  268. );
  269. /**
  270. * Add 'Home Top Background Image' Setting.
  271. */
  272. $wp_customize->add_setting(
  273. // $id
  274. 'pc_home_top_background_image',
  275. // $args
  276. array(
  277. 'default' => get_stylesheet_directory_uri() . '/images/bg-home.jpg',
  278. 'sanitize_callback' => 'theme_slug_sanitize_image',
  279. 'transport' => 'postMessage'
  280. )
  281. );
  282. /**
  283. * Add 'Home Top Background Image' image upload Control.
  284. */
  285. $wp_customize->add_control(
  286. new WP_Customize_Image_Control(
  287. // $wp_customize object
  288. $wp_customize,
  289. // $id
  290. 'pc_home_top_background_image',
  291. // $args
  292. array(
  293. 'settings' => 'pc_home_top_background_image',
  294. 'section' => 'pc_section_home_top',
  295. 'label' => __( 'Home Top Background Image', 'theme-slug' ),
  296. 'description' => __( 'Select the image to be used for Home Top Background.', 'theme-slug' )
  297. )
  298. )
  299. );
  300. }
  301. // Settings API options initilization and validation.
  302. add_action( 'customize_register', 'pc_register_theme_customizer' );
  303. /**
  304. * Writes the Home Top background image out to the 'head' element of the document
  305. * by reading the value from the theme mod value in the options table.
  306. */
  307. function pc_customizer_css() {
  308. ?>
  309. <style type="text/css">
  310. <?php if ( 0 < count( strlen( ( $home_top_background_image_url = get_theme_mod( 'pc_home_top_background_image' ) ) ) ) ) { ?>
  311. .home-top {
  312. background-image: url( <?php echo $home_top_background_image_url; ?> );
  313. }
  314. <?php } // end if ?>
  315. </style>
  316. <?php
  317. } // end pc_customizer_css
  318. add_action( 'wp_head', 'pc_customizer_css');
  319. /**
  320. * Registers the Theme Customizer Preview with WordPress.
  321. *
  322. * @package pc
  323. * @since 0.3.0
  324. * @version 0.3.0
  325. */
  326. function pc_customizer_live_preview() {
  327. wp_enqueue_script(
  328. 'pc-theme-customizer',
  329. get_stylesheet_directory_uri() . '/js/theme-customizer.js',
  330. array( 'customize-preview' ),
  331. '0.1.0',
  332. true
  333. );
  334. } // end pc_customizer_live_preview
  335. add_action( 'customize_preview_init', 'pc_customizer_live_preview' );
  336. //* Register widget areas
  337. genesis_register_sidebar( array(
  338. 'id' => 'home-subscribe-widget',
  339. 'name' => __( 'Home Subscribe Widget', 'prettycreative' ),
  340. 'description' => __( 'This is the full width subscription widget on the home page', 'prettycreative' ),
  341. ) );
  342. genesis_register_sidebar( array(
  343. 'id' => 'home-top',
  344. 'name' => __( 'Full Width Image', 'prettycreative' ),
  345. 'description' => __( 'This is the full width image area on your home page.', 'prettycreative' ),
  346. ) );
  347. genesis_register_sidebar( array(
  348. 'id' => 'home-page-1',
  349. 'name' => __( 'Home Page 1', 'prettycreative' ),
  350. 'description' => __( 'This is the Home Page 1 Widget.', 'prettycreative' ),
  351. ) );
  352. genesis_register_sidebar( array(
  353. 'id' => 'home-page-2',
  354. 'name' => __( 'Home Page 2', 'prettycreative' ),
  355. 'description' => __( 'This is the Home Page 2 Widget.', 'prettycreative' ),
  356. ) );
  357. genesis_register_sidebar( array(
  358. 'id' => 'home-page-3',
  359. 'name' => __( 'Home Page 3', 'prettycreative' ),
  360. 'description' => __( 'This is the Home Page 3 Widget.', 'prettycreative' ),
  361. ) );
  362. genesis_register_sidebar( array(
  363. 'id' => 'home-portfolio-widget',
  364. 'name' => __( 'Home Portfolio Widget', 'prettycreative' ),
  365. 'description' => __( 'This is the portfolio widget on your home page.', 'prettycreative' ),
  366. ) );
  367. genesis_register_sidebar( array(
  368. 'id' => 'nav-widget-area',
  369. 'name' => __( 'Nav Widget Area', 'prettycreative' ),
  370. 'description' => __( 'This widget appears in your navigation bar.', 'prettycreative' ),
  371. ) );
  372. genesis_register_sidebar( array(
  373. 'id' => 'author-box-custom',
  374. 'name' => __( 'Author Box Widget', 'prettycreative' ),
  375. 'description' => __( 'This is the Author Box Custom widget', 'prettycreative' ),
  376. ) );
  377. genesis_register_sidebar( array(
  378. 'id' => 'portfolioblurb',
  379. 'name' => __( 'Portfolio Archive Page Widget', 'prettycreative' ),
  380. 'description' => __( 'This widget appears above your portfolio items on your portfolio archive page.', 'prettycreative' ),
  381. ) );