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


Generated: Wed Oct 28 01:00:03 2020 Cross-referenced by PHPXref 0.7.1