wp/wp-content/themes/twentyfifteen/functions.php
changeset 8 c7c34916027a
parent 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
7:cf61fcea0001 8:c7c34916027a
     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://codex.wordpress.org/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://codex.wordpress.org/Function_Reference/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( array(
       
    85 		'primary' => __( 'Primary Menu',      'twentyfifteen' ),
       
    86 		'social'  => __( 'Social Links Menu', 'twentyfifteen' ),
       
    87 	) );
       
    88 
       
    89 	/*
       
    90 	 * Switch default core markup for search form, comment form, and comments
       
    91 	 * to output valid HTML5.
       
    92 	 */
       
    93 	add_theme_support( 'html5', array(
       
    94 		'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
       
    95 	) );
       
    96 
       
    97 	/*
       
    98 	 * Enable support for Post Formats.
       
    99 	 *
       
   100 	 * See: https://codex.wordpress.org/Post_Formats
       
   101 	 */
       
   102 	add_theme_support( 'post-formats', array(
       
   103 		'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
       
   104 	) );
       
   105 
       
   106 	/*
       
   107 	 * Enable support for custom logo.
       
   108 	 *
       
   109 	 * @since Twenty Fifteen 1.5
       
   110 	 */
       
   111 	add_theme_support( 'custom-logo', array(
       
   112 		'height'      => 248,
       
   113 		'width'       => 248,
       
   114 		'flex-height' => true,
       
   115 	) );
       
   116 
       
   117 	$color_scheme  = twentyfifteen_get_color_scheme();
       
   118 	$default_color = trim( $color_scheme[0], '#' );
       
   119 
       
   120 	// Setup the WordPress core custom background feature.
       
   121 
       
   122 	/**
       
   123 	 * Filter Twenty Fifteen custom-header support arguments.
       
   124 	 *
       
   125 	 * @since Twenty Fifteen 1.0
       
   126 	 *
       
   127 	 * @param array $args {
       
   128 	 *     An array of custom-header support arguments.
       
   129 	 *
       
   130 	 *     @type string $default-color     		Default color of the header.
       
   131 	 *     @type string $default-attachment     Default attachment of the header.
       
   132 	 * }
       
   133 	 */
       
   134 	add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
       
   135 		'default-color'      => $default_color,
       
   136 		'default-attachment' => 'fixed',
       
   137 	) ) );
       
   138 
       
   139 	/*
       
   140 	 * This theme styles the visual editor to resemble the theme style,
       
   141 	 * specifically font, colors, icons, and column width.
       
   142 	 */
       
   143 	add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
       
   144 
       
   145 	// Indicate widget sidebars can use selective refresh in the Customizer.
       
   146 	add_theme_support( 'customize-selective-refresh-widgets' );
       
   147 }
       
   148 endif; // twentyfifteen_setup
       
   149 add_action( 'after_setup_theme', 'twentyfifteen_setup' );
       
   150 
       
   151 /**
       
   152  * Register widget area.
       
   153  *
       
   154  * @since Twenty Fifteen 1.0
       
   155  *
       
   156  * @link https://codex.wordpress.org/Function_Reference/register_sidebar
       
   157  */
       
   158 function twentyfifteen_widgets_init() {
       
   159 	register_sidebar( array(
       
   160 		'name'          => __( 'Widget Area', 'twentyfifteen' ),
       
   161 		'id'            => 'sidebar-1',
       
   162 		'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
       
   163 		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
       
   164 		'after_widget'  => '</aside>',
       
   165 		'before_title'  => '<h2 class="widget-title">',
       
   166 		'after_title'   => '</h2>',
       
   167 	) );
       
   168 }
       
   169 add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
       
   170 
       
   171 if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
       
   172 /**
       
   173  * Register Google fonts for Twenty Fifteen.
       
   174  *
       
   175  * @since Twenty Fifteen 1.0
       
   176  *
       
   177  * @return string Google fonts URL for the theme.
       
   178  */
       
   179 function twentyfifteen_fonts_url() {
       
   180 	$fonts_url = '';
       
   181 	$fonts     = array();
       
   182 	$subsets   = 'latin,latin-ext';
       
   183 
       
   184 	/*
       
   185 	 * Translators: If there are characters in your language that are not supported
       
   186 	 * by Noto Sans, translate this to 'off'. Do not translate into your own language.
       
   187 	 */
       
   188 	if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
       
   189 		$fonts[] = 'Noto Sans:400italic,700italic,400,700';
       
   190 	}
       
   191 
       
   192 	/*
       
   193 	 * Translators: If there are characters in your language that are not supported
       
   194 	 * by Noto Serif, translate this to 'off'. Do not translate into your own language.
       
   195 	 */
       
   196 	if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
       
   197 		$fonts[] = 'Noto Serif:400italic,700italic,400,700';
       
   198 	}
       
   199 
       
   200 	/*
       
   201 	 * Translators: If there are characters in your language that are not supported
       
   202 	 * by Inconsolata, translate this to 'off'. Do not translate into your own language.
       
   203 	 */
       
   204 	if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
       
   205 		$fonts[] = 'Inconsolata:400,700';
       
   206 	}
       
   207 
       
   208 	/*
       
   209 	 * Translators: To add an additional character subset specific to your language,
       
   210 	 * translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language.
       
   211 	 */
       
   212 	$subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
       
   213 
       
   214 	if ( 'cyrillic' == $subset ) {
       
   215 		$subsets .= ',cyrillic,cyrillic-ext';
       
   216 	} elseif ( 'greek' == $subset ) {
       
   217 		$subsets .= ',greek,greek-ext';
       
   218 	} elseif ( 'devanagari' == $subset ) {
       
   219 		$subsets .= ',devanagari';
       
   220 	} elseif ( 'vietnamese' == $subset ) {
       
   221 		$subsets .= ',vietnamese';
       
   222 	}
       
   223 
       
   224 	if ( $fonts ) {
       
   225 		$fonts_url = add_query_arg( array(
       
   226 			'family' => urlencode( implode( '|', $fonts ) ),
       
   227 			'subset' => urlencode( $subsets ),
       
   228 		), 'https://fonts.googleapis.com/css' );
       
   229 	}
       
   230 
       
   231 	return $fonts_url;
       
   232 }
       
   233 endif;
       
   234 
       
   235 /**
       
   236  * JavaScript Detection.
       
   237  *
       
   238  * Adds a `js` class to the root `<html>` element when JavaScript is detected.
       
   239  *
       
   240  * @since Twenty Fifteen 1.1
       
   241  */
       
   242 function twentyfifteen_javascript_detection() {
       
   243 	echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
       
   244 }
       
   245 add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );
       
   246 
       
   247 /**
       
   248  * Enqueue scripts and styles.
       
   249  *
       
   250  * @since Twenty Fifteen 1.0
       
   251  */
       
   252 function twentyfifteen_scripts() {
       
   253 	// Add custom fonts, used in the main stylesheet.
       
   254 	wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
       
   255 
       
   256 	// Add Genericons, used in the main stylesheet.
       
   257 	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
       
   258 
       
   259 	// Load our main stylesheet.
       
   260 	wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
       
   261 
       
   262 	// Load the Internet Explorer specific stylesheet.
       
   263 	wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
       
   264 	wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
       
   265 
       
   266 	// Load the Internet Explorer 7 specific stylesheet.
       
   267 	wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
       
   268 	wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
       
   269 
       
   270 	wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
       
   271 
       
   272 	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
       
   273 		wp_enqueue_script( 'comment-reply' );
       
   274 	}
       
   275 
       
   276 	if ( is_singular() && wp_attachment_is_image() ) {
       
   277 		wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
       
   278 	}
       
   279 
       
   280 	wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150330', true );
       
   281 	wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
       
   282 		'expand'   => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
       
   283 		'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
       
   284 	) );
       
   285 }
       
   286 add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
       
   287 
       
   288 /**
       
   289  * Add preconnect for Google Fonts.
       
   290  *
       
   291  * @since Twenty Fifteen 1.7
       
   292  *
       
   293  * @param array   $urls          URLs to print for resource hints.
       
   294  * @param string  $relation_type The relation type the URLs are printed.
       
   295  * @return array URLs to print for resource hints.
       
   296  */
       
   297 function twentyfifteen_resource_hints( $urls, $relation_type ) {
       
   298 	if ( wp_style_is( 'twentyfifteen-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
       
   299 		if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '>=' ) ) {
       
   300 			$urls[] = array(
       
   301 				'href' => 'https://fonts.gstatic.com',
       
   302 				'crossorigin',
       
   303 			);
       
   304 		} else {
       
   305 			$urls[] = 'https://fonts.gstatic.com';
       
   306 		}
       
   307 	}
       
   308 
       
   309 	return $urls;
       
   310 }
       
   311 add_filter( 'wp_resource_hints', 'twentyfifteen_resource_hints', 10, 2 );
       
   312 
       
   313 /**
       
   314  * Add featured image as background image to post navigation elements.
       
   315  *
       
   316  * @since Twenty Fifteen 1.0
       
   317  *
       
   318  * @see wp_add_inline_style()
       
   319  */
       
   320 function twentyfifteen_post_nav_background() {
       
   321 	if ( ! is_single() ) {
       
   322 		return;
       
   323 	}
       
   324 
       
   325 	$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
       
   326 	$next     = get_adjacent_post( false, '', false );
       
   327 	$css      = '';
       
   328 
       
   329 	if ( is_attachment() && 'attachment' == $previous->post_type ) {
       
   330 		return;
       
   331 	}
       
   332 
       
   333 	if ( $previous &&  has_post_thumbnail( $previous->ID ) ) {
       
   334 		$prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
       
   335 		$css .= '
       
   336 			.post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
       
   337 			.post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
       
   338 			.post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
       
   339 		';
       
   340 	}
       
   341 
       
   342 	if ( $next && has_post_thumbnail( $next->ID ) ) {
       
   343 		$nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
       
   344 		$css .= '
       
   345 			.post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); border-top: 0; }
       
   346 			.post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
       
   347 			.post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
       
   348 		';
       
   349 	}
       
   350 
       
   351 	wp_add_inline_style( 'twentyfifteen-style', $css );
       
   352 }
       
   353 add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
       
   354 
       
   355 /**
       
   356  * Display descriptions in main navigation.
       
   357  *
       
   358  * @since Twenty Fifteen 1.0
       
   359  *
       
   360  * @param string  $item_output The menu item output.
       
   361  * @param WP_Post $item        Menu item object.
       
   362  * @param int     $depth       Depth of the menu.
       
   363  * @param array   $args        wp_nav_menu() arguments.
       
   364  * @return string Menu item with possible description.
       
   365  */
       
   366 function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
       
   367 	if ( 'primary' == $args->theme_location && $item->description ) {
       
   368 		$item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
       
   369 	}
       
   370 
       
   371 	return $item_output;
       
   372 }
       
   373 add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
       
   374 
       
   375 /**
       
   376  * Add a `screen-reader-text` class to the search form's submit button.
       
   377  *
       
   378  * @since Twenty Fifteen 1.0
       
   379  *
       
   380  * @param string $html Search form HTML.
       
   381  * @return string Modified search form HTML.
       
   382  */
       
   383 function twentyfifteen_search_form_modify( $html ) {
       
   384 	return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
       
   385 }
       
   386 add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );
       
   387 
       
   388 /**
       
   389  * Modifies tag cloud widget arguments to display all tags in the same font size
       
   390  * and use list format for better accessibility.
       
   391  *
       
   392  * @since Twenty Fifteen 1.9
       
   393  *
       
   394  * @param array $args Arguments for tag cloud widget.
       
   395  * @return array The filtered arguments for tag cloud widget.
       
   396  */
       
   397 function twentyfifteen_widget_tag_cloud_args( $args ) {
       
   398 	$args['largest']  = 22;
       
   399 	$args['smallest'] = 8;
       
   400 	$args['unit']     = 'pt';
       
   401 	$args['format']   = 'list';
       
   402 
       
   403 	return $args;
       
   404 }
       
   405 add_filter( 'widget_tag_cloud_args', 'twentyfifteen_widget_tag_cloud_args' );
       
   406 
       
   407 
       
   408 /**
       
   409  * Implement the Custom Header feature.
       
   410  *
       
   411  * @since Twenty Fifteen 1.0
       
   412  */
       
   413 require get_template_directory() . '/inc/custom-header.php';
       
   414 
       
   415 /**
       
   416  * Custom template tags for this theme.
       
   417  *
       
   418  * @since Twenty Fifteen 1.0
       
   419  */
       
   420 require get_template_directory() . '/inc/template-tags.php';
       
   421 
       
   422 /**
       
   423  * Customizer additions.
       
   424  *
       
   425  * @since Twenty Fifteen 1.0
       
   426  */
       
   427 require get_template_directory() . '/inc/customizer.php';