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


Generated: Tue Jan 19 01:00:03 2021 Cross-referenced by PHPXref 0.7.1