[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-content/themes/twentysixteen/ -> functions.php (source)

   1  <?php
   2  /**
   3   * Twenty Sixteen functions and definitions
   4   *
   5   * Set up the theme and provides some helper functions, which are used in the
   6   * theme as custom template tags. Others are attached to action and filter
   7   * hooks in WordPress to change core functionality.
   8   *
   9   * When using a child theme you can override certain functions (those wrapped
  10   * in a function_exists() call) by defining them first in your child theme's
  11   * functions.php file. The child theme's functions.php file is included before
  12   * the parent theme's file, so the child theme functions would be used.
  13   *
  14   * @link https://developer.wordpress.org/themes/basics/theme-functions/
  15   * @link https://developer.wordpress.org/themes/advanced-topics/child-themes/
  16   *
  17   * Functions that are not pluggable (not wrapped in function_exists()) are
  18   * instead attached to a filter or action hook.
  19   *
  20   * For more information on hooks, actions, and filters,
  21   * {@link https://developer.wordpress.org/plugins/}
  22   *
  23   * @package WordPress
  24   * @subpackage Twenty_Sixteen
  25   * @since Twenty Sixteen 1.0
  26   */
  27  
  28  /**
  29   * Twenty Sixteen only works in WordPress 4.4 or later.
  30   */
  31  if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) {
  32      require get_template_directory() . '/inc/back-compat.php';
  33  }
  34  
  35  if ( ! function_exists( 'twentysixteen_setup' ) ) :
  36      /**
  37       * Sets up theme defaults and registers support for various WordPress features.
  38       *
  39       * Note that this function is hooked into the after_setup_theme hook, which
  40       * runs before the init hook. The init hook is too late for some features, such
  41       * as indicating support for post thumbnails.
  42       *
  43       * Create your own twentysixteen_setup() function to override in a child theme.
  44       *
  45       * @since Twenty Sixteen 1.0
  46       */
  47  	function twentysixteen_setup() {
  48          /*
  49           * Make theme available for translation.
  50           * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentysixteen
  51           * If you're building a theme based on Twenty Sixteen, use a find and replace
  52           * to change 'twentysixteen' to the name of your theme in all the template files
  53           */
  54          load_theme_textdomain( 'twentysixteen' );
  55  
  56          // Add default posts and comments RSS feed links to head.
  57          add_theme_support( 'automatic-feed-links' );
  58  
  59          /*
  60           * Let WordPress manage the document title.
  61           * By adding theme support, we declare that this theme does not use a
  62           * hard-coded <title> tag in the document head, and expect WordPress to
  63           * provide it for us.
  64           */
  65          add_theme_support( 'title-tag' );
  66  
  67          /*
  68           * Enable support for custom logo.
  69           *
  70           *  @since Twenty Sixteen 1.2
  71           */
  72          add_theme_support(
  73              'custom-logo',
  74              array(
  75                  'height'      => 240,
  76                  'width'       => 240,
  77                  'flex-height' => true,
  78              )
  79          );
  80  
  81          /*
  82           * Enable support for Post Thumbnails on posts and pages.
  83           *
  84           * @link https://developer.wordpress.org/reference/functions/add_theme_support/#post-thumbnails
  85           */
  86          add_theme_support( 'post-thumbnails' );
  87          set_post_thumbnail_size( 1200, 9999 );
  88  
  89          // This theme uses wp_nav_menu() in two locations.
  90          register_nav_menus(
  91              array(
  92                  'primary' => __( 'Primary Menu', 'twentysixteen' ),
  93                  'social'  => __( 'Social Links Menu', 'twentysixteen' ),
  94              )
  95          );
  96  
  97          /*
  98           * Switch default core markup for search form, comment form, and comments
  99           * to output valid HTML5.
 100           */
 101          add_theme_support(
 102              'html5',
 103              array(
 104                  'search-form',
 105                  'comment-form',
 106                  'comment-list',
 107                  'gallery',
 108                  'caption',
 109              )
 110          );
 111  
 112          /*
 113           * Enable support for Post Formats.
 114           *
 115           * See: https://wordpress.org/support/article/post-formats/
 116           */
 117          add_theme_support(
 118              'post-formats',
 119              array(
 120                  'aside',
 121                  'image',
 122                  'video',
 123                  'quote',
 124                  'link',
 125                  'gallery',
 126                  'status',
 127                  'audio',
 128                  'chat',
 129              )
 130          );
 131  
 132          /*
 133           * This theme styles the visual editor to resemble the theme style,
 134           * specifically font, colors, icons, and column width.
 135           */
 136          add_editor_style( array( 'css/editor-style.css', twentysixteen_fonts_url() ) );
 137  
 138          // Load regular editor styles into the new block-based editor.
 139          add_theme_support( 'editor-styles' );
 140  
 141          // Load default block styles.
 142          add_theme_support( 'wp-block-styles' );
 143  
 144          // Add support for responsive embeds.
 145          add_theme_support( 'responsive-embeds' );
 146  
 147          // Add support for custom color scheme.
 148          add_theme_support(
 149              'editor-color-palette',
 150              array(
 151                  array(
 152                      'name'  => __( 'Dark Gray', 'twentysixteen' ),
 153                      'slug'  => 'dark-gray',
 154                      'color' => '#1a1a1a',
 155                  ),
 156                  array(
 157                      'name'  => __( 'Medium Gray', 'twentysixteen' ),
 158                      'slug'  => 'medium-gray',
 159                      'color' => '#686868',
 160                  ),
 161                  array(
 162                      'name'  => __( 'Light Gray', 'twentysixteen' ),
 163                      'slug'  => 'light-gray',
 164                      'color' => '#e5e5e5',
 165                  ),
 166                  array(
 167                      'name'  => __( 'White', 'twentysixteen' ),
 168                      'slug'  => 'white',
 169                      'color' => '#fff',
 170                  ),
 171                  array(
 172                      'name'  => __( 'Blue Gray', 'twentysixteen' ),
 173                      'slug'  => 'blue-gray',
 174                      'color' => '#4d545c',
 175                  ),
 176                  array(
 177                      'name'  => __( 'Bright Blue', 'twentysixteen' ),
 178                      'slug'  => 'bright-blue',
 179                      'color' => '#007acc',
 180                  ),
 181                  array(
 182                      'name'  => __( 'Light Blue', 'twentysixteen' ),
 183                      'slug'  => 'light-blue',
 184                      'color' => '#9adffd',
 185                  ),
 186                  array(
 187                      'name'  => __( 'Dark Brown', 'twentysixteen' ),
 188                      'slug'  => 'dark-brown',
 189                      'color' => '#402b30',
 190                  ),
 191                  array(
 192                      'name'  => __( 'Medium Brown', 'twentysixteen' ),
 193                      'slug'  => 'medium-brown',
 194                      'color' => '#774e24',
 195                  ),
 196                  array(
 197                      'name'  => __( 'Dark Red', 'twentysixteen' ),
 198                      'slug'  => 'dark-red',
 199                      'color' => '#640c1f',
 200                  ),
 201                  array(
 202                      'name'  => __( 'Bright Red', 'twentysixteen' ),
 203                      'slug'  => 'bright-red',
 204                      'color' => '#ff675f',
 205                  ),
 206                  array(
 207                      'name'  => __( 'Yellow', 'twentysixteen' ),
 208                      'slug'  => 'yellow',
 209                      'color' => '#ffef8e',
 210                  ),
 211              )
 212          );
 213  
 214          // Indicate widget sidebars can use selective refresh in the Customizer.
 215          add_theme_support( 'customize-selective-refresh-widgets' );
 216      }
 217  endif; // twentysixteen_setup
 218  add_action( 'after_setup_theme', 'twentysixteen_setup' );
 219  
 220  /**
 221   * Sets the content width in pixels, based on the theme's design and stylesheet.
 222   *
 223   * Priority 0 to make it available to lower priority callbacks.
 224   *
 225   * @global int $content_width
 226   *
 227   * @since Twenty Sixteen 1.0
 228   */
 229  function twentysixteen_content_width() {
 230      $GLOBALS['content_width'] = apply_filters( 'twentysixteen_content_width', 840 );
 231  }
 232  add_action( 'after_setup_theme', 'twentysixteen_content_width', 0 );
 233  
 234  /**
 235   * Add preconnect for Google Fonts.
 236   *
 237   * @since Twenty Sixteen 1.6
 238   *
 239   * @param array  $urls           URLs to print for resource hints.
 240   * @param string $relation_type  The relation type the URLs are printed.
 241   * @return array $urls           URLs to print for resource hints.
 242   */
 243  function twentysixteen_resource_hints( $urls, $relation_type ) {
 244      if ( wp_style_is( 'twentysixteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
 245          $urls[] = array(
 246              'href' => 'https://fonts.gstatic.com',
 247              'crossorigin',
 248          );
 249      }
 250  
 251      return $urls;
 252  }
 253  add_filter( 'wp_resource_hints', 'twentysixteen_resource_hints', 10, 2 );
 254  
 255  /**
 256   * Registers a widget area.
 257   *
 258   * @link https://developer.wordpress.org/reference/functions/register_sidebar/
 259   *
 260   * @since Twenty Sixteen 1.0
 261   */
 262  function twentysixteen_widgets_init() {
 263      register_sidebar(
 264          array(
 265              'name'          => __( 'Sidebar', 'twentysixteen' ),
 266              'id'            => 'sidebar-1',
 267              'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ),
 268              'before_widget' => '<section id="%1$s" class="widget %2$s">',
 269              'after_widget'  => '</section>',
 270              'before_title'  => '<h2 class="widget-title">',
 271              'after_title'   => '</h2>',
 272          )
 273      );
 274  
 275      register_sidebar(
 276          array(
 277              'name'          => __( 'Content Bottom 1', 'twentysixteen' ),
 278              'id'            => 'sidebar-2',
 279              'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
 280              'before_widget' => '<section id="%1$s" class="widget %2$s">',
 281              'after_widget'  => '</section>',
 282              'before_title'  => '<h2 class="widget-title">',
 283              'after_title'   => '</h2>',
 284          )
 285      );
 286  
 287      register_sidebar(
 288          array(
 289              'name'          => __( 'Content Bottom 2', 'twentysixteen' ),
 290              'id'            => 'sidebar-3',
 291              'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'twentysixteen' ),
 292              'before_widget' => '<section id="%1$s" class="widget %2$s">',
 293              'after_widget'  => '</section>',
 294              'before_title'  => '<h2 class="widget-title">',
 295              'after_title'   => '</h2>',
 296          )
 297      );
 298  }
 299  add_action( 'widgets_init', 'twentysixteen_widgets_init' );
 300  
 301  if ( ! function_exists( 'twentysixteen_fonts_url' ) ) :
 302      /**
 303       * Register Google fonts for Twenty Sixteen.
 304       *
 305       * Create your own twentysixteen_fonts_url() function to override in a child theme.
 306       *
 307       * @since Twenty Sixteen 1.0
 308       *
 309       * @return string Google fonts URL for the theme.
 310       */
 311  	function twentysixteen_fonts_url() {
 312          $fonts_url = '';
 313          $fonts     = array();
 314          $subsets   = 'latin,latin-ext';
 315  
 316          /*
 317           * translators: If there are characters in your language that are not supported
 318           * by Merriweather, translate this to 'off'. Do not translate into your own language.
 319           */
 320          if ( 'off' !== _x( 'on', 'Merriweather font: on or off', 'twentysixteen' ) ) {
 321              $fonts[] = 'Merriweather:400,700,900,400italic,700italic,900italic';
 322          }
 323  
 324          /*
 325           * translators: If there are characters in your language that are not supported
 326           * by Montserrat, translate this to 'off'. Do not translate into your own language.
 327           */
 328          if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'twentysixteen' ) ) {
 329              $fonts[] = 'Montserrat:400,700';
 330          }
 331  
 332          /*
 333           * translators: If there are characters in your language that are not supported
 334           * by Inconsolata, translate this to 'off'. Do not translate into your own language.
 335           */
 336          if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentysixteen' ) ) {
 337              $fonts[] = 'Inconsolata:400';
 338          }
 339  
 340          if ( $fonts ) {
 341              $fonts_url = add_query_arg(
 342                  array(
 343                      'family'  => urlencode( implode( '|', $fonts ) ),
 344                      'subset'  => urlencode( $subsets ),
 345                      'display' => urlencode( 'fallback' ),
 346                  ),
 347                  'https://fonts.googleapis.com/css'
 348              );
 349          }
 350  
 351          return $fonts_url;
 352      }
 353  endif;
 354  
 355  /**
 356   * Handles JavaScript detection.
 357   *
 358   * Adds a `js` class to the root `<html>` element when JavaScript is detected.
 359   *
 360   * @since Twenty Sixteen 1.0
 361   */
 362  function twentysixteen_javascript_detection() {
 363      echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
 364  }
 365  add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );
 366  
 367  /**
 368   * Enqueues scripts and styles.
 369   *
 370   * @since Twenty Sixteen 1.0
 371   */
 372  function twentysixteen_scripts() {
 373      // Add custom fonts, used in the main stylesheet.
 374      wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );
 375  
 376      // Add Genericons, used in the main stylesheet.
 377      wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );
 378  
 379      // Theme stylesheet.
 380      wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri(), array(), '20190507' );
 381  
 382      // Theme block stylesheet.
 383      wp_enqueue_style( 'twentysixteen-block-style', get_template_directory_uri() . '/css/blocks.css', array( 'twentysixteen-style' ), '20190102' );
 384  
 385      // Load the Internet Explorer specific stylesheet.
 386      wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20170530' );
 387      wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' );
 388  
 389      // Load the Internet Explorer 8 specific stylesheet.
 390      wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20170530' );
 391      wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' );
 392  
 393      // Load the Internet Explorer 7 specific stylesheet.
 394      wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20170530' );
 395      wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' );
 396  
 397      // Load the html5 shiv.
 398      wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' );
 399      wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' );
 400  
 401      wp_enqueue_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20170530', true );
 402  
 403      if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
 404          wp_enqueue_script( 'comment-reply' );
 405      }
 406  
 407      if ( is_singular() && wp_attachment_is_image() ) {
 408          wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20170530' );
 409      }
 410  
 411      wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20181217', true );
 412  
 413      wp_localize_script(
 414          'twentysixteen-script',
 415          'screenReaderText',
 416          array(
 417              'expand'   => __( 'expand child menu', 'twentysixteen' ),
 418              'collapse' => __( 'collapse child menu', 'twentysixteen' ),
 419          )
 420      );
 421  }
 422  add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' );
 423  
 424  /**
 425   * Enqueue styles for the block-based editor.
 426   *
 427   * @since Twenty Sixteen 1.6
 428   */
 429  function twentysixteen_block_editor_styles() {
 430      // Block styles.
 431      wp_enqueue_style( 'twentysixteen-block-editor-style', get_template_directory_uri() . '/css/editor-blocks.css', array(), '20190102' );
 432      // Add custom fonts.
 433      wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );
 434  }
 435  add_action( 'enqueue_block_editor_assets', 'twentysixteen_block_editor_styles' );
 436  
 437  /**
 438   * Adds custom classes to the array of body classes.
 439   *
 440   * @since Twenty Sixteen 1.0
 441   *
 442   * @param array $classes Classes for the body element.
 443   * @return array (Maybe) filtered body classes.
 444   */
 445  function twentysixteen_body_classes( $classes ) {
 446      // Adds a class of custom-background-image to sites with a custom background image.
 447      if ( get_background_image() ) {
 448          $classes[] = 'custom-background-image';
 449      }
 450  
 451      // Adds a class of group-blog to sites with more than 1 published author.
 452      if ( is_multi_author() ) {
 453          $classes[] = 'group-blog';
 454      }
 455  
 456      // Adds a class of no-sidebar to sites without active sidebar.
 457      if ( ! is_active_sidebar( 'sidebar-1' ) ) {
 458          $classes[] = 'no-sidebar';
 459      }
 460  
 461      // Adds a class of hfeed to non-singular pages.
 462      if ( ! is_singular() ) {
 463          $classes[] = 'hfeed';
 464      }
 465  
 466      return $classes;
 467  }
 468  add_filter( 'body_class', 'twentysixteen_body_classes' );
 469  
 470  /**
 471   * Converts a HEX value to RGB.
 472   *
 473   * @since Twenty Sixteen 1.0
 474   *
 475   * @param string $color The original color, in 3- or 6-digit hexadecimal form.
 476   * @return array Array containing RGB (red, green, and blue) values for the given
 477   *               HEX code, empty array otherwise.
 478   */
 479  function twentysixteen_hex2rgb( $color ) {
 480      $color = trim( $color, '#' );
 481  
 482      if ( strlen( $color ) === 3 ) {
 483          $r = hexdec( substr( $color, 0, 1 ) . substr( $color, 0, 1 ) );
 484          $g = hexdec( substr( $color, 1, 1 ) . substr( $color, 1, 1 ) );
 485          $b = hexdec( substr( $color, 2, 1 ) . substr( $color, 2, 1 ) );
 486      } elseif ( strlen( $color ) === 6 ) {
 487          $r = hexdec( substr( $color, 0, 2 ) );
 488          $g = hexdec( substr( $color, 2, 2 ) );
 489          $b = hexdec( substr( $color, 4, 2 ) );
 490      } else {
 491          return array();
 492      }
 493  
 494      return array(
 495          'red'   => $r,
 496          'green' => $g,
 497          'blue'  => $b,
 498      );
 499  }
 500  
 501  /**
 502   * Custom template tags for this theme.
 503   */
 504  require get_template_directory() . '/inc/template-tags.php';
 505  
 506  /**
 507   * Customizer additions.
 508   */
 509  require get_template_directory() . '/inc/customizer.php';
 510  
 511  /**
 512   * Add custom image sizes attribute to enhance responsive image functionality
 513   * for content images
 514   *
 515   * @since Twenty Sixteen 1.0
 516   *
 517   * @param string $sizes A source size value for use in a 'sizes' attribute.
 518   * @param array  $size  Image size. Accepts an array of width and height
 519   *                      values in pixels (in that order).
 520   * @return string A source size value for use in a content image 'sizes' attribute.
 521   */
 522  function twentysixteen_content_image_sizes_attr( $sizes, $size ) {
 523      $width = $size[0];
 524  
 525      if ( 840 <= $width ) {
 526          $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px';
 527      }
 528  
 529      if ( 'page' === get_post_type() ) {
 530          if ( 840 > $width ) {
 531              $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
 532          }
 533      } else {
 534          if ( 840 > $width && 600 <= $width ) {
 535              $sizes = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px';
 536          } elseif ( 600 > $width ) {
 537              $sizes = '(max-width: ' . $width . 'px) 85vw, ' . $width . 'px';
 538          }
 539      }
 540  
 541      return $sizes;
 542  }
 543  add_filter( 'wp_calculate_image_sizes', 'twentysixteen_content_image_sizes_attr', 10, 2 );
 544  
 545  /**
 546   * Add custom image sizes attribute to enhance responsive image functionality
 547   * for post thumbnails
 548   *
 549   * @since Twenty Sixteen 1.0
 550   *
 551   * @param array $attr Attributes for the image markup.
 552   * @param int   $attachment Image attachment ID.
 553   * @param array $size Registered image size or flat array of height and width dimensions.
 554   * @return array The filtered attributes for the image markup.
 555   */
 556  function twentysixteen_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
 557      if ( 'post-thumbnail' === $size ) {
 558          if ( is_active_sidebar( 'sidebar-1' ) ) {
 559              $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 60vw, (max-width: 1362px) 62vw, 840px';
 560          } else {
 561              $attr['sizes'] = '(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 88vw, 1200px';
 562          }
 563      }
 564      return $attr;
 565  }
 566  add_filter( 'wp_get_attachment_image_attributes', 'twentysixteen_post_thumbnail_sizes_attr', 10, 3 );
 567  
 568  /**
 569   * Modifies tag cloud widget arguments to display all tags in the same font size
 570   * and use list format for better accessibility.
 571   *
 572   * @since Twenty Sixteen 1.1
 573   *
 574   * @param array $args Arguments for tag cloud widget.
 575   * @return array The filtered arguments for tag cloud widget.
 576   */
 577  function twentysixteen_widget_tag_cloud_args( $args ) {
 578      $args['largest']  = 1;
 579      $args['smallest'] = 1;
 580      $args['unit']     = 'em';
 581      $args['format']   = 'list';
 582  
 583      return $args;
 584  }
 585  add_filter( 'widget_tag_cloud_args', 'twentysixteen_widget_tag_cloud_args' );


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1