Advertisement
Guest User

Functions WordPress 2015 theme custom

a guest
Mar 9th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.01 KB | None | 0 0
  1.             'family' => urlencode( implode( '|', $fonts ) ),
  2.             'subset' => urlencode( $subsets ),
  3.         ), 'https://fonts.googleapis.com/css' );
  4.     }
  5.  
  6.     return $fonts_url;
  7. }
  8. endif;
  9.  
  10. /**
  11.  * Handles JavaScript detection.
  12.  *
  13.  * Adds a `js` class to the root `<html>` element when JavaScript is detected.
  14.  *
  15.  * @since Twenty Sixteen 1.0
  16.  */
  17. function twentysixteen_javascript_detection() {
  18.     echo "&lt;script&gt;(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);&lt;/script&gt;\n";
  19. }
  20. add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );
  21.  
  22. /**
  23.  * Enqueues scripts and styles.
  24.  *
  25.  * @since Twenty Sixteen 1.0
  26.  */
  27. function twentysixteen_scripts() {
  28.     // Add custom fonts, used in the main stylesheet.
  29.     wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );
  30.  
  31.     // Add Genericons, used in the main stylesheet.
  32.     wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );
  33.  
  34.     // Theme stylesheet.
  35.     wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri() );
  36.  
  37.     // Load the Internet Explorer specific stylesheet.
  38.     wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20160816' );
  39.     wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' );
  40.  
  41.     // Load the Internet Explorer 8 specific stylesheet.
  42.     wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20160816' );
  43.     wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' );
  44.  
  45.     // Load the Internet Explorer 7 specific stylesheet.
  46.     wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20160816' );
  47.     wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' );
  48.  
  49.     // Load the html5 shiv.
  50.     wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' );
  51.     wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' );
  52.  
  53.     wp_enqueue_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20160816', true );
  54.  
  55.     if ( is_singular() &amp;&amp; comments_open() &amp;&amp; get_option( 'thread_comments' ) ) {
  56.         wp_enqueue_script( 'comment-reply' );
  57.     }
  58.  
  59.     if ( is_singular() &amp;&amp; wp_attachment_is_image() ) {
  60.         wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20160816' );
  61.     }
  62.  
  63.     wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20160816', true );
  64.  
  65.     wp_localize_script( 'twentysixteen-script', 'screenReaderText', array(
  66.         'expand'   =&gt; __( 'expand child menu', 'twentysixteen' ),
  67.         'collapse' =&gt; __( 'collapse child menu', 'twentysixteen' ),
  68.     ) );
  69. }
  70. add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' );
  71.  
  72. /**
  73.  * Adds custom classes to the array of body classes.
  74.  *
  75.  * @since Twenty Sixteen 1.0
  76.  *
  77.  * @param array $classes Classes for the body element.
  78.  * @return array (Maybe) filtered body classes.
  79.  */
  80. function twentysixteen_body_classes( $classes ) {
  81.     // Adds a class of custom-background-image to sites with a custom background image.
  82.     if ( get_background_image() ) {
  83.         $classes[] = 'custom-background-image';
  84.     }
  85.  
  86.     // Adds a class of group-blog to sites with more than 1 published author.
  87.     if ( is_multi_author() ) {
  88.         $classes[] = 'group-blog';
  89.     }
  90.  
  91.     // Adds a class of no-sidebar to sites without active sidebar.
  92.     if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  93.         $classes[] = 'no-sidebar';
  94.     }
  95.  
  96.     // Adds a class of hfeed to non-singular pages.
  97.     if ( ! is_singular() ) {
  98.         $classes[] = 'hfeed';
  99.     }
  100.  
  101.     return $classes;
  102. }
  103. add_filter( 'body_class', 'twentysixteen_body_classes' );
  104.  
  105. /**
  106.  * Converts a HEX value to RGB.
  107.  *
  108.  * @since Twenty Sixteen 1.0
  109.  *
  110.  * @param string $color The original color, in 3- or 6-digit hexadecimal form.
  111.  * @return array Array containing RGB (red, green, and blue) values for the given
  112.  *               HEX code, empty array otherwise.
  113.  */
  114. function twentysixteen_hex2rgb( $color ) {
  115.     $color = trim( $color, '#' );
  116.  
  117.     if ( strlen( $color ) === 3 ) {
  118.         $r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
  119.         $g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
  120.         $b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
  121.     } else if ( strlen( $color ) === 6 ) {
  122.         $r = hexdec( substr( $color, 0, 2 ) );
  123.         $g = hexdec( substr( $color, 2, 2 ) );
  124.         $b = hexdec( substr( $color, 4, 2 ) );
  125.     } else {
  126.         return array();
  127.     }
  128.  
  129.     return array( 'red' =&gt; $r, 'green' =&gt; $g, 'blue' =&gt; $b );
  130. }
  131.  
  132. /**
  133.  * Custom template tags for this theme.
  134.  */
  135. require get_template_directory() . '/inc/template-tags.php';
  136.  
  137. /**
  138.  * Customizer additions.
  139.  */
  140. require get_template_directory() . '/inc/customizer.php';
  141.  
  142. /**
  143.  * Add custom image sizes attribute to enhance responsive image functionality
  144.  * for content images
  145.  *
  146.  * @since Twenty Sixteen 1.0
  147.  *
  148.  * @param string $sizes A source size value for use in a 'sizes' attribute.
  149.  * @param array  $size  Image size. Accepts an array of width and height
  150.  *                      values in pixels (in that order).
  151.  * @return string A source size value for use in a content image 'sizes' attribute.
  152.  */
  153. function twentysixteen_content_image_sizes_attr( $sizes, $size ) {
  154.     $width = $size[0];
  155.  
  156.     840 &lt;= $width &amp;&amp; $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
  157.  
  158.     if ( 'page' === get_post_type() ) {
  159.         840 &gt; $width &amp;&amp; $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
  160.     } else {
  161.         840 &gt; $width &amp;&amp; 600 &lt;= $width &amp;&amp; $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px';
  162.         600 &gt; $width &amp;&amp; $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
  163.     }
  164.  
  165.     return $sizes;
  166. }
  167. add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10 , 2 );
  168.  
  169. /**
  170.  * Add custom image sizes attribute to enhance responsive image functionality
  171.  * for post thumbnails
  172.  *
  173.  * @since Twenty Sixteen 1.0
  174.  *
  175.  * @param array $attr Attributes for the image markup.
  176.  * @param int   $attachment Image attachment ID.
  177.  * @param array $size Registered image size or flat array of height and width dimensions.
  178.  * @return string A source size value for use in a post thumbnail 'sizes' attribute.
  179.  */
  180. function twentysixteen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
  181.     if ( 'post-thumbnail' === $size ) {
  182.         is_active_sidebar( 'sidebar-1' ) &amp;&amp; $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px';
  183.         ! is_active_sidebar( 'sidebar-1' ) &amp;&amp; $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px';
  184.     }
  185.     return $attr;
  186. }
  187. add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10 , 3 );
  188.  
  189. /**
  190.  * Modifies tag cloud widget arguments to have all tags in the widget same font size.
  191.  *
  192.  * @since Twenty Sixteen 1.1
  193.  *
  194.  * @param array $args Arguments for tag cloud widget.
  195.  * @return array A new modified arguments.
  196.  */
  197. function twentysixteen_widget_tag_cloud_args( $args ) {
  198.     $args['largest'] = 1;
  199.     $args['smallest'] = 1;
  200.     $args['unit'] = 'em';
  201.     return $args;
  202. }
  203. add_filter( 'widget_tag_cloud_args', 'twentysixteen_widget_tag_cloud_args' );
  204.  
  205. /**
  206. * Remove Post Thumbnail
  207. *
  208. * Remove post thumbnail from single post but in preview
  209. *
  210. * @param string $thumbnail_html The thumbnail markup
  211. *
  212. * @return string Empty string or the original parameter value
  213. */
  214. function my_remove_post_thumbnail_html($thumbnail_html)
  215. {
  216. if (is_single() && !is_preview() || is_category() && !is_preview()) {
  217. $thumbnail_html =;
  218. }
  219.  
  220. return $thumbnail_html;
  221. }
  222. add_filter(‘post_thumbnail_html’, ‘my_remove_post_thumbnail_html’);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement