[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7