[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Twenty Nineteen functions and definitions
   4   *
   5   * @link https://developer.wordpress.org/themes/basics/theme-functions/
   6   *
   7   * @package WordPress
   8   * @subpackage Twenty_Nineteen
   9   * @since Twenty Nineteen 1.0
  10   */
  11  
  12  /**
  13   * Twenty Nineteen only works in WordPress 4.7 or later.
  14   */
  15  if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
  16      require get_template_directory() . '/inc/back-compat.php';
  17      return;
  18  }
  19  
  20  if ( ! function_exists( 'twentynineteen_setup' ) ) :
  21      /**
  22       * Sets up theme defaults and registers support for various WordPress features.
  23       *
  24       * Note that this function is hooked into the after_setup_theme hook, which
  25       * runs before the init hook. The init hook is too late for some features, such
  26       * as indicating support for post thumbnails.
  27       */
  28  	function twentynineteen_setup() {
  29  
  30          // Add default posts and comments RSS feed links to head.
  31          add_theme_support( 'automatic-feed-links' );
  32  
  33          /*
  34           * Let WordPress manage the document title.
  35           * By adding theme support, we declare that this theme does not use a
  36           * hard-coded <title> tag in the document head, and expect WordPress to
  37           * provide it for us.
  38           */
  39          add_theme_support( 'title-tag' );
  40  
  41          /*
  42           * Enable support for Post Thumbnails on posts and pages.
  43           *
  44           * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  45           */
  46          add_theme_support( 'post-thumbnails' );
  47          set_post_thumbnail_size( 1568, 9999 );
  48  
  49          // This theme uses wp_nav_menu() in two locations.
  50          register_nav_menus(
  51              array(
  52                  'menu-1' => __( 'Primary', 'twentynineteen' ),
  53                  'footer' => __( 'Footer Menu', 'twentynineteen' ),
  54                  'social' => __( 'Social Links Menu', 'twentynineteen' ),
  55              )
  56          );
  57  
  58          /*
  59           * Switch default core markup for search form, comment form, and comments
  60           * to output valid HTML5.
  61           */
  62          add_theme_support(
  63              'html5',
  64              array(
  65                  'search-form',
  66                  'comment-form',
  67                  'comment-list',
  68                  'gallery',
  69                  'caption',
  70                  'script',
  71                  'style',
  72                  'navigation-widgets',
  73              )
  74          );
  75  
  76          /**
  77           * Add support for core custom logo.
  78           *
  79           * @link https://codex.wordpress.org/Theme_Logo
  80           */
  81          add_theme_support(
  82              'custom-logo',
  83              array(
  84                  'height'      => 190,
  85                  'width'       => 190,
  86                  'flex-width'  => false,
  87                  'flex-height' => false,
  88              )
  89          );
  90  
  91          // Add theme support for selective refresh for widgets.
  92          add_theme_support( 'customize-selective-refresh-widgets' );
  93  
  94          // Add support for Block Styles.
  95          add_theme_support( 'wp-block-styles' );
  96  
  97          // Add support for full and wide align images.
  98          add_theme_support( 'align-wide' );
  99  
 100          // Add support for editor styles.
 101          add_theme_support( 'editor-styles' );
 102  
 103          // Enqueue editor styles.
 104          add_editor_style( 'style-editor.css' );
 105  
 106          // Add custom editor font sizes.
 107          add_theme_support(
 108              'editor-font-sizes',
 109              array(
 110                  array(
 111                      'name'      => __( 'Small', 'twentynineteen' ),
 112                      'shortName' => __( 'S', 'twentynineteen' ),
 113                      'size'      => 19.5,
 114                      'slug'      => 'small',
 115                  ),
 116                  array(
 117                      'name'      => __( 'Normal', 'twentynineteen' ),
 118                      'shortName' => __( 'M', 'twentynineteen' ),
 119                      'size'      => 22,
 120                      'slug'      => 'normal',
 121                  ),
 122                  array(
 123                      'name'      => __( 'Large', 'twentynineteen' ),
 124                      'shortName' => __( 'L', 'twentynineteen' ),
 125                      'size'      => 36.5,
 126                      'slug'      => 'large',
 127                  ),
 128                  array(
 129                      'name'      => __( 'Huge', 'twentynineteen' ),
 130                      'shortName' => __( 'XL', 'twentynineteen' ),
 131                      'size'      => 49.5,
 132                      'slug'      => 'huge',
 133                  ),
 134              )
 135          );
 136  
 137          // Editor color palette.
 138          add_theme_support(
 139              'editor-color-palette',
 140              array(
 141                  array(
 142                      'name'  => 'default' === get_theme_mod( 'primary_color', 'default' ) ? __( 'Blue', 'twentynineteen' ) : null,
 143                      'slug'  => 'primary',
 144                      'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 33 ),
 145                  ),
 146                  array(
 147                      'name'  => 'default' === get_theme_mod( 'primary_color', 'default' ) ? __( 'Dark Blue', 'twentynineteen' ) : null,
 148                      'slug'  => 'secondary',
 149                      'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 23 ),
 150                  ),
 151                  array(
 152                      'name'  => __( 'Dark Gray', 'twentynineteen' ),
 153                      'slug'  => 'dark-gray',
 154                      'color' => '#111',
 155                  ),
 156                  array(
 157                      'name'  => __( 'Light Gray', 'twentynineteen' ),
 158                      'slug'  => 'light-gray',
 159                      'color' => '#767676',
 160                  ),
 161                  array(
 162                      'name'  => __( 'White', 'twentynineteen' ),
 163                      'slug'  => 'white',
 164                      'color' => '#FFF',
 165                  ),
 166              )
 167          );
 168  
 169          // Add support for responsive embedded content.
 170          add_theme_support( 'responsive-embeds' );
 171  
 172          // Add support for custom line height.
 173          add_theme_support( 'custom-line-height' );
 174      }
 175  endif;
 176  add_action( 'after_setup_theme', 'twentynineteen_setup' );
 177  
 178  if ( ! function_exists( 'wp_get_list_item_separator' ) ) :
 179      /**
 180       * Retrieves the list item separator based on the locale.
 181       *
 182       * Added for backward compatibility to support pre-6.0.0 WordPress versions.
 183       *
 184       * @since Twenty Nineteen 2.3
 185       *
 186       * @return string Locale-specific list item separator.
 187       */
 188  	function wp_get_list_item_separator() {
 189          /* translators: Used between list items, there is a space after the comma. */
 190          return __( ', ', 'twentynineteen' );
 191      }
 192  endif;
 193  
 194  /**
 195   * Registers widget area.
 196   *
 197   * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 198   */
 199  function twentynineteen_widgets_init() {
 200  
 201      register_sidebar(
 202          array(
 203              'name'          => __( 'Footer', 'twentynineteen' ),
 204              'id'            => 'sidebar-1',
 205              'description'   => __( 'Add widgets here to appear in your footer.', 'twentynineteen' ),
 206              'before_widget' => '<section id="%1$s" class="widget %2$s">',
 207              'after_widget'  => '</section>',
 208              'before_title'  => '<h2 class="widget-title">',
 209              'after_title'   => '</h2>',
 210          )
 211      );
 212  }
 213  add_action( 'widgets_init', 'twentynineteen_widgets_init' );
 214  
 215  /**
 216   * Replaces "[...]" (appended to automatically generated excerpts) with ... and
 217   * a 'Continue reading' link.
 218   *
 219   * @since Twenty Nineteen 2.0
 220   *
 221   * @param string $link Link to single post/page.
 222   * @return string 'Continue reading' link prepended with an ellipsis.
 223   */
 224  function twentynineteen_excerpt_more( $link ) {
 225      if ( is_admin() ) {
 226          return $link;
 227      }
 228  
 229      $link = sprintf(
 230          '<p class="link-more"><a href="%1$s" class="more-link">%2$s</a></p>',
 231          esc_url( get_permalink( get_the_ID() ) ),
 232          /* translators: %s: Post title. Only visible to screen readers. */
 233          sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentynineteen' ), get_the_title( get_the_ID() ) )
 234      );
 235      return ' &hellip; ' . $link;
 236  }
 237  add_filter( 'excerpt_more', 'twentynineteen_excerpt_more' );
 238  
 239  /**
 240   * Sets the content width in pixels, based on the theme's design and stylesheet.
 241   *
 242   * Priority 0 to make it available to lower priority callbacks.
 243   *
 244   * @global int $content_width Content width.
 245   */
 246  function twentynineteen_content_width() {
 247      /**
 248       * Filters Twenty Nineteen content width of the theme.
 249       *
 250       * @since Twenty Nineteen 1.0
 251       *
 252       * @param int $content_width Content width in pixels.
 253       */
 254      $GLOBALS['content_width'] = apply_filters( 'twentynineteen_content_width', 640 );
 255  }
 256  add_action( 'after_setup_theme', 'twentynineteen_content_width', 0 );
 257  
 258  /**
 259   * Enqueues scripts and styles.
 260   *
 261   * @since Twenty Nineteen 1.0
 262   */
 263  function twentynineteen_scripts() {
 264      wp_enqueue_style( 'twentynineteen-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
 265  
 266      wp_style_add_data( 'twentynineteen-style', 'rtl', 'replace' );
 267  
 268      if ( has_nav_menu( 'menu-1' ) ) {
 269          wp_enqueue_script(
 270              'twentynineteen-priority-menu',
 271              get_theme_file_uri( '/js/priority-menu.js' ),
 272              array(),
 273              '20200129',
 274              array(
 275                  'in_footer' => false, // Because involves header.
 276                  'strategy'  => 'defer',
 277              )
 278          );
 279          wp_enqueue_script(
 280              'twentynineteen-touch-navigation',
 281              get_theme_file_uri( '/js/touch-keyboard-navigation.js' ),
 282              array(),
 283              '20250802',
 284              array(
 285                  'in_footer' => true,
 286                  'strategy'  => 'defer',
 287              )
 288          );
 289      }
 290  
 291      wp_enqueue_style( 'twentynineteen-print-style', get_template_directory_uri() . '/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
 292  
 293      if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
 294          wp_enqueue_script( 'comment-reply' );
 295      }
 296  }
 297  add_action( 'wp_enqueue_scripts', 'twentynineteen_scripts' );
 298  
 299  /**
 300   * Fixes skip link focus in IE11.
 301   *
 302   * This does not enqueue the script because it is tiny and because it is only for IE11,
 303   * thus it does not warrant having an entire dedicated blocking script being loaded.
 304   *
 305   * @since Twenty Nineteen 1.0
 306   * @deprecated Twenty Nineteen 2.6 Removed from wp_print_footer_scripts action.
 307   *
 308   * @link https://git.io/vWdr2
 309   */
 310  function twentynineteen_skip_link_focus_fix() {
 311      // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
 312      ?>
 313      <script>
 314      /(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
 315      </script>
 316      <?php
 317  }
 318  
 319  /**
 320   * Enqueues supplemental block editor styles.
 321   */
 322  function twentynineteen_editor_customizer_styles() {
 323  
 324      wp_enqueue_style( 'twentynineteen-editor-customizer-styles', get_theme_file_uri( '/style-editor-customizer.css' ), false, '2.1', 'all' );
 325  
 326      if ( 'custom' === get_theme_mod( 'primary_color' ) ) {
 327          // Include color patterns.
 328          require_once get_parent_theme_file_path( '/inc/color-patterns.php' );
 329          wp_add_inline_style( 'twentynineteen-editor-customizer-styles', twentynineteen_custom_colors_css() );
 330      }
 331  }
 332  add_action( 'enqueue_block_editor_assets', 'twentynineteen_editor_customizer_styles' );
 333  
 334  /**
 335   * Displays custom color CSS in customizer and on frontend.
 336   */
 337  function twentynineteen_colors_css_wrap() {
 338  
 339      // Only include custom colors in customizer or frontend.
 340      if ( ( ! is_customize_preview() && 'default' === get_theme_mod( 'primary_color', 'default' ) ) || is_admin() ) {
 341          return;
 342      }
 343  
 344      require_once get_parent_theme_file_path( '/inc/color-patterns.php' );
 345  
 346      $primary_color = 199;
 347      if ( 'default' !== get_theme_mod( 'primary_color', 'default' ) ) {
 348          $primary_color = get_theme_mod( 'primary_color_hue', 199 );
 349      }
 350      ?>
 351  
 352      <style id="custom-theme-colors" <?php echo is_customize_preview() ? 'data-hue="' . absint( $primary_color ) . '"' : ''; ?>>
 353          <?php echo twentynineteen_custom_colors_css(); ?>
 354      </style>
 355      <?php
 356  }
 357  add_action( 'wp_head', 'twentynineteen_colors_css_wrap' );
 358  
 359  /**
 360   * SVG Icons class.
 361   */
 362  require get_template_directory() . '/classes/class-twentynineteen-svg-icons.php';
 363  
 364  /**
 365   * Custom Comment Walker template.
 366   */
 367  require get_template_directory() . '/classes/class-twentynineteen-walker-comment.php';
 368  
 369  /**
 370   * Common theme functions.
 371   */
 372  require get_template_directory() . '/inc/helper-functions.php';
 373  
 374  /**
 375   * SVG Icons related functions.
 376   */
 377  require get_template_directory() . '/inc/icon-functions.php';
 378  
 379  /**
 380   * Enhance the theme by hooking into WordPress.
 381   */
 382  require get_template_directory() . '/inc/template-functions.php';
 383  
 384  /**
 385   * Custom template tags for the theme.
 386   */
 387  require get_template_directory() . '/inc/template-tags.php';
 388  
 389  /**
 390   * Customizer additions.
 391   */
 392  require get_template_directory() . '/inc/customizer.php';
 393  
 394  /**
 395   * Registers block patterns and pattern categories.
 396   *
 397   * @since Twenty Nineteen 3.0
 398   */
 399  function twentynineteen_register_block_patterns() {
 400      require get_template_directory() . '/inc/block-patterns.php';
 401  }
 402  
 403  add_action( 'init', 'twentynineteen_register_block_patterns' );


Generated : Sat Apr 18 08:20:10 2026 Cross-referenced by PHPXref