[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Twenty Fifteen 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_Fifteen
  25   * @since Twenty Fifteen 1.0
  26   */
  27  
  28  /**
  29   * Set the content width based on the theme's design and stylesheet.
  30   *
  31   * @since Twenty Fifteen 1.0
  32   */
  33  if ( ! isset( $content_width ) ) {
  34      $content_width = 660;
  35  }
  36  
  37  /**
  38   * Twenty Fifteen only works in WordPress 4.1 or later.
  39   */
  40  if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
  41      require get_template_directory() . '/inc/back-compat.php';
  42  }
  43  
  44  if ( ! function_exists( 'twentyfifteen_setup' ) ) :
  45      /**
  46       * Sets up theme defaults and registers support for various WordPress features.
  47       *
  48       * Note that this function is hooked into the after_setup_theme hook, which
  49       * runs before the init hook. The init hook is too late for some features, such
  50       * as indicating support for post thumbnails.
  51       *
  52       * @since Twenty Fifteen 1.0
  53       */
  54  	function twentyfifteen_setup() {
  55  
  56          /*
  57           * Make theme available for translation.
  58           * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentyfifteen
  59           * If you're building a theme based on twentyfifteen, use a find and replace
  60           * to change 'twentyfifteen' to the name of your theme in all the template files
  61           */
  62          load_theme_textdomain( 'twentyfifteen' );
  63  
  64          // Add default posts and comments RSS feed links to head.
  65          add_theme_support( 'automatic-feed-links' );
  66  
  67          /*
  68           * Let WordPress manage the document title.
  69           * By adding theme support, we declare that this theme does not use a
  70           * hard-coded <title> tag in the document head, and expect WordPress to
  71           * provide it for us.
  72           */
  73          add_theme_support( 'title-tag' );
  74  
  75          /*
  76           * Enable support for Post Thumbnails on posts and pages.
  77           *
  78           * See: https://developer.wordpress.org/reference/functions/add_theme_support/#post-thumbnails
  79           */
  80          add_theme_support( 'post-thumbnails' );
  81          set_post_thumbnail_size( 825, 510, true );
  82  
  83          // This theme uses wp_nav_menu() in two locations.
  84          register_nav_menus(
  85              array(
  86                  'primary' => __( 'Primary Menu', 'twentyfifteen' ),
  87                  'social'  => __( 'Social Links Menu', 'twentyfifteen' ),
  88              )
  89          );
  90  
  91          /*
  92           * Switch default core markup for search form, comment form, and comments
  93           * to output valid HTML5.
  94           */
  95          add_theme_support(
  96              'html5',
  97              array(
  98                  'search-form',
  99                  'comment-form',
 100                  'comment-list',
 101                  'gallery',
 102                  'caption',
 103                  'script',
 104                  'style',
 105              )
 106          );
 107  
 108          /*
 109           * Enable support for Post Formats.
 110           *
 111           * See: https://wordpress.org/support/article/post-formats/
 112           */
 113          add_theme_support(
 114              'post-formats',
 115              array(
 116                  'aside',
 117                  'image',
 118                  'video',
 119                  'quote',
 120                  'link',
 121                  'gallery',
 122                  'status',
 123                  'audio',
 124                  'chat',
 125              )
 126          );
 127  
 128          /*
 129           * Enable support for custom logo.
 130           *
 131           * @since Twenty Fifteen 1.5
 132           */
 133          add_theme_support(
 134              'custom-logo',
 135              array(
 136                  'height'      => 248,
 137                  'width'       => 248,
 138                  'flex-height' => true,
 139              )
 140          );
 141  
 142          $color_scheme  = twentyfifteen_get_color_scheme();
 143          $default_color = trim( $color_scheme[0], '#' );
 144  
 145          // Setup the WordPress core custom background feature.
 146  
 147          /**
 148           * Filter Twenty Fifteen custom-header support arguments.
 149           *
 150           * @since Twenty Fifteen 1.0
 151           *
 152           * @param array $args {
 153           *     An array of custom-header support arguments.
 154           *
 155           *     @type string $default-color          Default color of the header.
 156           *     @type string $default-attachment     Default attachment of the header.
 157           * }
 158           */
 159          add_theme_support(
 160              'custom-background',
 161              apply_filters(
 162                  'twentyfifteen_custom_background_args',
 163                  array(
 164                      'default-color'      => $default_color,
 165                      'default-attachment' => 'fixed',
 166                  )
 167              )
 168          );
 169  
 170          /*
 171           * This theme styles the visual editor to resemble the theme style,
 172           * specifically font, colors, icons, and column width.
 173           */
 174          add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
 175  
 176          // Load regular editor styles into the new block-based editor.
 177          add_theme_support( 'editor-styles' );
 178  
 179          // Load default block styles.
 180          add_theme_support( 'wp-block-styles' );
 181  
 182          // Add support for responsive embeds.
 183          add_theme_support( 'responsive-embeds' );
 184  
 185          // Add support for custom color scheme.
 186          add_theme_support(
 187              'editor-color-palette',
 188              array(
 189                  array(
 190                      'name'  => __( 'Dark Gray', 'twentyfifteen' ),
 191                      'slug'  => 'dark-gray',
 192                      'color' => '#111',
 193                  ),
 194                  array(
 195                      'name'  => __( 'Light Gray', 'twentyfifteen' ),
 196                      'slug'  => 'light-gray',
 197                      'color' => '#f1f1f1',
 198                  ),
 199                  array(
 200                      'name'  => __( 'White', 'twentyfifteen' ),
 201                      'slug'  => 'white',
 202                      'color' => '#fff',
 203                  ),
 204                  array(
 205                      'name'  => __( 'Yellow', 'twentyfifteen' ),
 206                      'slug'  => 'yellow',
 207                      'color' => '#f4ca16',
 208                  ),
 209                  array(
 210                      'name'  => __( 'Dark Brown', 'twentyfifteen' ),
 211                      'slug'  => 'dark-brown',
 212                      'color' => '#352712',
 213                  ),
 214                  array(
 215                      'name'  => __( 'Medium Pink', 'twentyfifteen' ),
 216                      'slug'  => 'medium-pink',
 217                      'color' => '#e53b51',
 218                  ),
 219                  array(
 220                      'name'  => __( 'Light Pink', 'twentyfifteen' ),
 221                      'slug'  => 'light-pink',
 222                      'color' => '#ffe5d1',
 223                  ),
 224                  array(
 225                      'name'  => __( 'Dark Purple', 'twentyfifteen' ),
 226                      'slug'  => 'dark-purple',
 227                      'color' => '#2e2256',
 228                  ),
 229                  array(
 230                      'name'  => __( 'Purple', 'twentyfifteen' ),
 231                      'slug'  => 'purple',
 232                      'color' => '#674970',
 233                  ),
 234                  array(
 235                      'name'  => __( 'Blue Gray', 'twentyfifteen' ),
 236                      'slug'  => 'blue-gray',
 237                      'color' => '#22313f',
 238                  ),
 239                  array(
 240                      'name'  => __( 'Bright Blue', 'twentyfifteen' ),
 241                      'slug'  => 'bright-blue',
 242                      'color' => '#55c3dc',
 243                  ),
 244                  array(
 245                      'name'  => __( 'Light Blue', 'twentyfifteen' ),
 246                      'slug'  => 'light-blue',
 247                      'color' => '#e9f2f9',
 248                  ),
 249              )
 250          );
 251  
 252          // Indicate widget sidebars can use selective refresh in the Customizer.
 253          add_theme_support( 'customize-selective-refresh-widgets' );
 254      }
 255  endif; // twentyfifteen_setup
 256  add_action( 'after_setup_theme', 'twentyfifteen_setup' );
 257  
 258  /**
 259   * Register widget area.
 260   *
 261   * @since Twenty Fifteen 1.0
 262   *
 263   * @link https://developer.wordpress.org/reference/functions/register_sidebar/
 264   */
 265  function twentyfifteen_widgets_init() {
 266      register_sidebar(
 267          array(
 268              'name'          => __( 'Widget Area', 'twentyfifteen' ),
 269              'id'            => 'sidebar-1',
 270              'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
 271              'before_widget' => '<aside id="%1$s" class="widget %2$s">',
 272              'after_widget'  => '</aside>',
 273              'before_title'  => '<h2 class="widget-title">',
 274              'after_title'   => '</h2>',
 275          )
 276      );
 277  }
 278  add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
 279  
 280  if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
 281      /**
 282       * Register Google fonts for Twenty Fifteen.
 283       *
 284       * @since Twenty Fifteen 1.0
 285       *
 286       * @return string Google fonts URL for the theme.
 287       */
 288  	function twentyfifteen_fonts_url() {
 289          $fonts_url = '';
 290          $fonts     = array();
 291          $subsets   = 'latin,latin-ext';
 292  
 293          /*
 294           * translators: If there are characters in your language that are not supported
 295           * by Noto Sans, translate this to 'off'. Do not translate into your own language.
 296           */
 297          if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
 298              $fonts[] = 'Noto Sans:400italic,700italic,400,700';
 299          }
 300  
 301          /*
 302           * translators: If there are characters in your language that are not supported
 303           * by Noto Serif, translate this to 'off'. Do not translate into your own language.
 304           */
 305          if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
 306              $fonts[] = 'Noto Serif:400italic,700italic,400,700';
 307          }
 308  
 309          /*
 310           * translators: If there are characters in your language that are not supported
 311           * by Inconsolata, translate this to 'off'. Do not translate into your own language.
 312           */
 313          if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
 314              $fonts[] = 'Inconsolata:400,700';
 315          }
 316  
 317          /*
 318           * translators: To add an additional character subset specific to your language,
 319           * translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language.
 320           */
 321          $subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
 322  
 323          if ( 'cyrillic' == $subset ) {
 324              $subsets .= ',cyrillic,cyrillic-ext';
 325          } elseif ( 'greek' == $subset ) {
 326              $subsets .= ',greek,greek-ext';
 327          } elseif ( 'devanagari' == $subset ) {
 328              $subsets .= ',devanagari';
 329          } elseif ( 'vietnamese' == $subset ) {
 330              $subsets .= ',vietnamese';
 331          }
 332  
 333          if ( $fonts ) {
 334              $fonts_url = add_query_arg(
 335                  array(
 336                      'family'  => urlencode( implode( '|', $fonts ) ),
 337                      'subset'  => urlencode( $subsets ),
 338                      'display' => urlencode( 'fallback' ),
 339                  ),
 340                  'https://fonts.googleapis.com/css'
 341              );
 342          }
 343  
 344          return $fonts_url;
 345      }
 346  endif;
 347  
 348  /**
 349   * JavaScript Detection.
 350   *
 351   * Adds a `js` class to the root `<html>` element when JavaScript is detected.
 352   *
 353   * @since Twenty Fifteen 1.1
 354   */
 355  function twentyfifteen_javascript_detection() {
 356      echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
 357  }
 358  add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );
 359  
 360  /**
 361   * Enqueue scripts and styles.
 362   *
 363   * @since Twenty Fifteen 1.0
 364   */
 365  function twentyfifteen_scripts() {
 366      // Add custom fonts, used in the main stylesheet.
 367      wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
 368  
 369      // Add Genericons, used in the main stylesheet.
 370      wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
 371  
 372      // Load our main stylesheet.
 373      wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri(), array(), '20190507' );
 374  
 375      // Theme block stylesheet.
 376      wp_enqueue_style( 'twentyfifteen-block-style', get_template_directory_uri() . '/css/blocks.css', array( 'twentyfifteen-style' ), '20190102' );
 377  
 378      // Load the Internet Explorer specific stylesheet.
 379      wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20170916' );
 380      wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
 381  
 382      // Load the Internet Explorer 7 specific stylesheet.
 383      wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141210' );
 384      wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
 385  
 386      wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141028', true );
 387  
 388      if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
 389          wp_enqueue_script( 'comment-reply' );
 390      }
 391  
 392      if ( is_singular() && wp_attachment_is_image() ) {
 393          wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141210' );
 394      }
 395  
 396      wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20171218', true );
 397      wp_localize_script(
 398          'twentyfifteen-script',
 399          'screenReaderText',
 400          array(
 401              'expand'   => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
 402              'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
 403          )
 404      );
 405  }
 406  add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
 407  
 408  /**
 409   * Enqueue styles for the block-based editor.
 410   *
 411   * @since Twenty Fifteen 2.1
 412   */
 413  function twentyfifteen_block_editor_styles() {
 414      // Block styles.
 415      wp_enqueue_style( 'twentyfifteen-block-editor-style', get_template_directory_uri() . '/css/editor-blocks.css', array(), '20190102' );
 416      // Add custom fonts.
 417      wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
 418  }
 419  add_action( 'enqueue_block_editor_assets', 'twentyfifteen_block_editor_styles' );
 420  
 421  
 422  /**
 423   * Add preconnect for Google Fonts.
 424   *
 425   * @since Twenty Fifteen 1.7
 426   *
 427   * @param array   $urls          URLs to print for resource hints.
 428   * @param string  $relation_type The relation type the URLs are printed.
 429   * @return array URLs to print for resource hints.
 430   */
 431  function twentyfifteen_resource_hints( $urls, $relation_type ) {
 432      if ( wp_style_is( 'twentyfifteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
 433          if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '>=' ) ) {
 434              $urls[] = array(
 435                  'href' => 'https://fonts.gstatic.com',
 436                  'crossorigin',
 437              );
 438          } else {
 439              $urls[] = 'https://fonts.gstatic.com';
 440          }
 441      }
 442  
 443      return $urls;
 444  }
 445  add_filter( 'wp_resource_hints', 'twentyfifteen_resource_hints', 10, 2 );
 446  
 447  /**
 448   * Add featured image as background image to post navigation elements.
 449   *
 450   * @since Twenty Fifteen 1.0
 451   *
 452   * @see wp_add_inline_style()
 453   */
 454  function twentyfifteen_post_nav_background() {
 455      if ( ! is_single() ) {
 456          return;
 457      }
 458  
 459      $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
 460      $next     = get_adjacent_post( false, '', false );
 461      $css      = '';
 462  
 463      if ( is_attachment() && 'attachment' == $previous->post_type ) {
 464          return;
 465      }
 466  
 467      if ( $previous && has_post_thumbnail( $previous->ID ) ) {
 468          $prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
 469          $css      .= '
 470              .post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
 471              .post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
 472              .post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
 473          ';
 474      }
 475  
 476      if ( $next && has_post_thumbnail( $next->ID ) ) {
 477          $nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
 478          $css      .= '
 479              .post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); border-top: 0; }
 480              .post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
 481              .post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
 482          ';
 483      }
 484  
 485      wp_add_inline_style( 'twentyfifteen-style', $css );
 486  }
 487  add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
 488  
 489  /**
 490   * Display descriptions in main navigation.
 491   *
 492   * @since Twenty Fifteen 1.0
 493   *
 494   * @param string  $item_output The menu item output.
 495   * @param WP_Post $item        Menu item object.
 496   * @param int     $depth       Depth of the menu.
 497   * @param array   $args        wp_nav_menu() arguments.
 498   * @return string Menu item with possible description.
 499   */
 500  function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
 501      if ( 'primary' == $args->theme_location && $item->description ) {
 502          $item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
 503      }
 504  
 505      return $item_output;
 506  }
 507  add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
 508  
 509  /**
 510   * Add a `screen-reader-text` class to the search form's submit button.
 511   *
 512   * @since Twenty Fifteen 1.0
 513   *
 514   * @param string $html Search form HTML.
 515   * @return string Modified search form HTML.
 516   */
 517  function twentyfifteen_search_form_modify( $html ) {
 518      return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
 519  }
 520  add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );
 521  
 522  /**
 523   * Modifies tag cloud widget arguments to display all tags in the same font size
 524   * and use list format for better accessibility.
 525   *
 526   * @since Twenty Fifteen 1.9
 527   *
 528   * @param array $args Arguments for tag cloud widget.
 529   * @return array The filtered arguments for tag cloud widget.
 530   */
 531  function twentyfifteen_widget_tag_cloud_args( $args ) {
 532      $args['largest']  = 22;
 533      $args['smallest'] = 8;
 534      $args['unit']     = 'pt';
 535      $args['format']   = 'list';
 536  
 537      return $args;
 538  }
 539  add_filter( 'widget_tag_cloud_args', 'twentyfifteen_widget_tag_cloud_args' );
 540  
 541  /**
 542   * Prevents `author-bio.php` partial template from interfering with rendering
 543   * an author archive of a user with the `bio` username.
 544   *
 545   * @since Twenty Fifteen 2.6
 546   *
 547   * @param string $template Template file.
 548   * @return string Replacement template file.
 549   */
 550  function twentyfifteen_author_bio_template( $template ) {
 551      if ( is_author() ) {
 552          $author = get_queried_object();
 553          if ( $author instanceof WP_User && 'bio' === $author->user_nicename ) {
 554              // Use author templates if exist, fall back to template hierarchy otherwise.
 555              return locate_template( array( "author-{$author->ID}.php", 'author.php' ) );
 556          }
 557      }
 558  
 559      return $template;
 560  }
 561  add_filter( 'author_template', 'twentyfifteen_author_bio_template' );
 562  
 563  
 564  /**
 565   * Implement the Custom Header feature.
 566   *
 567   * @since Twenty Fifteen 1.0
 568   */
 569  require get_template_directory() . '/inc/custom-header.php';
 570  
 571  /**
 572   * Custom template tags for this theme.
 573   *
 574   * @since Twenty Fifteen 1.0
 575   */
 576  require get_template_directory() . '/inc/template-tags.php';
 577  
 578  /**
 579   * Customizer additions.
 580   *
 581   * @since Twenty Fifteen 1.0
 582   */
 583  require get_template_directory() . '/inc/customizer.php';


Generated: Sat Sep 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1