Posted By

chrisaiv on 09/13/14


Tagged

wordpress


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

artofzen
chrisaiv


Wordpress: boilerplate functions.php


 / Published in: PHP
 

The functions here are a compendium of snippets I've posted in the past. I will continue to update this file regularly.

  1. /** ******** ******** ******** ******** ******** ******** ******** ********
  2. * Faster than @import
  3. *
  4. * http://codex.wordpress.org/Customizing_the_Login_Form
  5. *
  6. */
  7.  
  8. function my_child_theme_scripts() {
  9. wp_enqueue_style( 'parent-theme-css', get_template_directory_uri() . '/style.css' );
  10. }
  11. add_action( 'wp_enqueue_scripts', 'my_child_theme_scripts' );
  12.  
  13.  
  14. /** ******** ******** ******** ******** ******** ******** ******** ********
  15. * Customize Favicon
  16. *
  17. * http://codex.wordpress.org/Customizing_the_Login_Form
  18. *
  19. */
  20.  
  21. // Custom Function to Include
  22. function favicon_link() {
  23. echo '<link rel="shortcut icon" type="image/x-icon" href="' . get_stylesheet_directory() .'/favicons/favicon.ico" />' . "\n";
  24. }
  25. add_action( 'wp_head', 'favicon_link' );
  26.  
  27.  
  28. /** ******** ******** ******** ******** ******** ******** ******** ********
  29. * Title: Remove Black Baar on mobile
  30. *
  31. * http://codex.wordpress.org/Roles_and_Capabilities#Subscriber
  32. * http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/
  33. *
  34. * @param string $role Role name.
  35. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  36. * @return bool
  37. */
  38. if (!current_user_can('edit_posts') || wp_is_mobile() ) {
  39. show_admin_bar(false);
  40. }
  41.  
  42. /** ******** ******** ******** ******** ******** ******** ******** ********
  43. * TITLE: Change admin e-mail to new users
  44. * DESCRIPTION: auto-detect the server so you only have to enter the front/from
  45. * half of the email address, including the @ sign
  46. *
  47. * http://snipplr.com/view/77687/wordpress-change-admin-email-to-new-users/
  48. * http://miloguide.com/filter-hooks/wp_mail_from_name/
  49. * http://premium.wpmudev.org/blog/wordpress-email-settings/
  50. *
  51. */
  52. function admin_email_filter_wp_mail_from($email){
  53. /* start of code lifted from wordpress core, at
  54. http://svn.automattic.com/wordpress/tags/3.4/wp-includes/pluggable.php */
  55. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  56. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  57. $sitename = substr( $sitename, 4 );
  58. }
  59. /* end of code lifted from wordpress core */
  60. $myfront = "[email protected]";
  61. $myback = $sitename;
  62. $myfrom = $myfront . $myback;
  63. return $myfrom;
  64. }
  65. add_filter("wp_mail_from", "admin_email_filter_wp_mail_from");
  66.  
  67. /**
  68. * TITLE: Change admin name to new users
  69. * DESCRIPTION: enter the full name you want displayed alongside the email address
  70. *
  71. * http://miloguide.com/filter-hooks/wp_mail_from_name/
  72. */
  73.  
  74.  
  75. function admin_name_filter_wp_mail_from_name($from_name){
  76. return "GuitarPick";
  77. }
  78. add_filter("wp_mail_from_name", "admin_name_filter_wp_mail_from_name");
  79.  
  80.  
  81. /** ******** ******** ******** ******** ******** ******** ******** ********
  82. * TITLE: Beautify the Log-in Page
  83. * DESCRIPTION: Make the Log-in Page PRetty
  84. * 1: Change out Logo
  85. * 2: Change out URL
  86. * 3: Change out Title
  87. *
  88. * http://codex.wordpress.org/Customizing_the_Login_Form
  89. */
  90. function my_login_logo() { ?>
  91. <style type="text/css">
  92. body.login div#login h1 a {
  93. background-image: none;
  94. }
  95. </style>
  96. <?php }
  97. add_action( 'login_enqueue_scripts', 'my_login_logo' );
  98.  
  99. function my_login_logo_url() {
  100. return home_url();
  101. }
  102. add_filter( 'login_headerurl', 'my_login_logo_url' );
  103.  
  104. function my_login_logo_url_title() {
  105. return 'GuitarPick';
  106. }
  107. add_filter( 'login_headertitle', 'my_login_logo_url_title' );
  108.  
  109. function no_errors_please(){
  110. return 'Please Try Again';
  111. }
  112. add_filter( 'login_errors', 'no_errors_please' );
  113.  
  114. /** ******** ******** ******** ******** ******** ******** ******** ********
  115. * TITLE: Remove unnecessary meta-data from your WordPress site
  116. * DESCRIPTION: Over the years, wordpress has placed a lot of junk in their system.
  117. */
  118. function remove_header_extra(){
  119. //Are you editing your WordPress blog using your browser? Then you are not using a blog client
  120. remove_action('wp_head', 'rsd_link');
  121. // Windows Live Writer is (it�s another blog editing client
  122. remove_action('wp_head', 'wlwmanifest_link');
  123. //This announces that you are running WordPress and what version you are using.
  124. remove_action('wp_head', 'wp_generator');
  125. //URL shortening is sometimes useful, but this automatic ugly url in your header is useless.
  126. remove_action('wp_head', 'wp_shortlink_wp_head');
  127. // Display the links to the general feeds: Post and Comment Feed
  128. remove_action('wp_head', 'feed_links', 2);
  129. //// Display the links to the extra feeds such as category feeds
  130. remove_action('wp_head', 'feed_links_extra', 3);
  131. //Deprecated
  132. remove_action('wp_head', 'index_rel_link');
  133. // start link
  134. remove_action('wp_head', 'start_post_rel_link', 10, 0);
  135. // prev link
  136. remove_action('wp_head', 'parent_post_rel_link', 10, 0);
  137. // Display relational links for the posts adjacent to the current post.
  138. remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
  139. }
  140. add_action('init', 'remove_header_extra');
  141.  
  142. /** ******** ******** ******** ******** ******** ******** ******** ********
  143. * Title: Wordpress Dashboard
  144. * Description: Remove all those boxes on the Wordpress Dashboard
  145. *
  146. * http://codex.wordpress.org/Function_Reference/remove_meta_box
  147. */
  148. function remove_dashboard_meta() {
  149. remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
  150. remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
  151. remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
  152. remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
  153. remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
  154. remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
  155. remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
  156. remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
  157. remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
  158. remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
  159. remove_meta_box( 'dashboard_welcome_panel', 'dashboard', 'normal' );
  160. }
  161. add_action( 'admin_init', 'remove_dashboard_meta' );
  162.  
  163.  
  164. /** ******** ******** ******** ******** ******** ******** ******** ********
  165. * Title: Limit The Excerpt�s Word Count
  166. *
  167. * http://www.smashingmagazine.com/2011/12/07/10-tips-optimize-wordpress-theme/
  168. *
  169. *
  170. */
  171. function ilc_excerpt_length( $length ){
  172. return 10;
  173. }
  174. add_filter('excerpt_length', 'ilc_excerpt_length');
  175.  
  176.  
  177. /** ******** ******** ******** ******** ******** ******** ******** ********
  178. * Title: Remove Black Baar on mobile
  179. *
  180. * http://codex.wordpress.org/Roles_and_Capabilities#Subscriber
  181. * http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/
  182. *
  183. * @param string $role Role name.
  184. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  185. * @return bool
  186. */
  187. if (!current_user_can('edit_posts') || wp_is_mobile() ) {
  188. show_admin_bar(false);
  189. }
  190.  
  191. /** ******** ******** ******** ******** ******** ******** ******** ********
  192. * Title: Redirect Wordpress Feeds to Feedburner
  193. *
  194. * http://www.smashingmagazine.com/2011/12/07/10-tips-optimize-wordpress-theme/
  195. */
  196. function feed_to_feedburner_redirect() {
  197. if ( is_feed() && !preg_match('/feedburner|feedvalidator/i', $_SERVER['HTTP_USER_AGENT'])){
  198. header('Location: http://feeds.feedburner.com/[THE_NAME_OF_YOUR_FEED]');
  199. header('HTTP/1.1 302 Temporary Redirect');
  200. }
  201. }
  202. //add_action('template_redirect', 'feed_to_feedburner_redirect');
  203.  
  204. /** ******** ******** ******** ******** ******** ******** ******** ********
  205. * Title: Remove fields from woocommerce checkout
  206. *
  207. * [Override Billing](https://gist.github.com/mikejolley/1860056)
  208. * [WooCommerce Plug-ins and hacks](http://wordpress.org/support/topic/woocommerce-remove-fields-on-edit-address)
  209. *
  210. */
  211.  
  212. // Our hooked in function - $fields is passed via the filter!
  213. function custom_override_checkout_fields( $fields ) {
  214. unset($fields['order']['order_comments']);
  215. unset($fields['billing']['billing_phone']);
  216. unset($fields['billing']['billing_address_2']);
  217. unset($fields['billing']['billing_company']);
  218. unset($fields['shipping']['shipping_company']);
  219. unset($fields['shipping']['shipping_address_2']);
  220.  
  221. return $fields;
  222. }
  223. add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
  224.  
  225.  
  226. /** ******** ******** ******** ******** ******** ******** ******** ********
  227. * Title: Make Woocommerce Phone Field Not Required
  228. *
  229. * [Override Billing](https://gist.github.com/mikejolley/1860056)
  230. * [WooCommerce Plug-ins and hacks](http://wordpress.org/support/topic/woocommerce-remove-fields-on-edit-address)
  231. *
  232. */
  233.  
  234. function wc_npr_filter_phone( $address_fields ) {
  235. $address_fields['billing_phone']['required'] = false;
  236. return $address_fields;
  237. }
  238. add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
  239.  
  240. /** ******** ******** ******** ******** ******** ******** ******** ********
  241. * Title: Show Featured Images In Feed
  242. *
  243. * http://www.smashingmagazine.com/2011/12/07/10-tips-optimize-wordpress-theme/
  244. *
  245. *
  246. */
  247.  
  248. function rss_post_thumbnail($content) {
  249. global $post;
  250. if( has_post_thumbnail($post->ID) )
  251. $content = '<p>' . get_the_post_thumbnail($post->ID, 'thumbnail') . '</p>' . $content;
  252. return $content;
  253. }
  254. add_filter('the_content_feed', 'rss_post_thumbnail');
  255.  
  256. /** ******** ******** ******** ******** ******** ******** ******** ********
  257. *
  258. */

Report this snippet  

You need to login to post a comment.