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


Generated: Sun Jul 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1