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


Generated: Sat Nov 28 01:00:03 2020 Cross-referenced by PHPXref 0.7.1