wp/wp-content/themes/twentyfifteen/functions.php
changeset 5 5e2f62d02dcd
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     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 in the /languages/ directory.
       
    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', get_template_directory() . '/languages' );
       
    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 	$color_scheme  = twentyfifteen_get_color_scheme();
       
   107 	$default_color = trim( $color_scheme[0], '#' );
       
   108 
       
   109 	// Setup the WordPress core custom background feature.
       
   110 	add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
       
   111 		'default-color'      => $default_color,
       
   112 		'default-attachment' => 'fixed',
       
   113 	) ) );
       
   114 
       
   115 	/*
       
   116 	 * This theme styles the visual editor to resemble the theme style,
       
   117 	 * specifically font, colors, icons, and column width.
       
   118 	 */
       
   119 	add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
       
   120 }
       
   121 endif; // twentyfifteen_setup
       
   122 add_action( 'after_setup_theme', 'twentyfifteen_setup' );
       
   123 
       
   124 /**
       
   125  * Register widget area.
       
   126  *
       
   127  * @since Twenty Fifteen 1.0
       
   128  *
       
   129  * @link https://codex.wordpress.org/Function_Reference/register_sidebar
       
   130  */
       
   131 function twentyfifteen_widgets_init() {
       
   132 	register_sidebar( array(
       
   133 		'name'          => __( 'Widget Area', 'twentyfifteen' ),
       
   134 		'id'            => 'sidebar-1',
       
   135 		'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
       
   136 		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
       
   137 		'after_widget'  => '</aside>',
       
   138 		'before_title'  => '<h2 class="widget-title">',
       
   139 		'after_title'   => '</h2>',
       
   140 	) );
       
   141 }
       
   142 add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
       
   143 
       
   144 if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
       
   145 /**
       
   146  * Register Google fonts for Twenty Fifteen.
       
   147  *
       
   148  * @since Twenty Fifteen 1.0
       
   149  *
       
   150  * @return string Google fonts URL for the theme.
       
   151  */
       
   152 function twentyfifteen_fonts_url() {
       
   153 	$fonts_url = '';
       
   154 	$fonts     = array();
       
   155 	$subsets   = 'latin,latin-ext';
       
   156 
       
   157 	/*
       
   158 	 * Translators: If there are characters in your language that are not supported
       
   159 	 * by Noto Sans, translate this to 'off'. Do not translate into your own language.
       
   160 	 */
       
   161 	if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
       
   162 		$fonts[] = 'Noto Sans:400italic,700italic,400,700';
       
   163 	}
       
   164 
       
   165 	/*
       
   166 	 * Translators: If there are characters in your language that are not supported
       
   167 	 * by Noto Serif, translate this to 'off'. Do not translate into your own language.
       
   168 	 */
       
   169 	if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
       
   170 		$fonts[] = 'Noto Serif:400italic,700italic,400,700';
       
   171 	}
       
   172 
       
   173 	/*
       
   174 	 * Translators: If there are characters in your language that are not supported
       
   175 	 * by Inconsolata, translate this to 'off'. Do not translate into your own language.
       
   176 	 */
       
   177 	if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
       
   178 		$fonts[] = 'Inconsolata:400,700';
       
   179 	}
       
   180 
       
   181 	/*
       
   182 	 * Translators: To add an additional character subset specific to your language,
       
   183 	 * translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language.
       
   184 	 */
       
   185 	$subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
       
   186 
       
   187 	if ( 'cyrillic' == $subset ) {
       
   188 		$subsets .= ',cyrillic,cyrillic-ext';
       
   189 	} elseif ( 'greek' == $subset ) {
       
   190 		$subsets .= ',greek,greek-ext';
       
   191 	} elseif ( 'devanagari' == $subset ) {
       
   192 		$subsets .= ',devanagari';
       
   193 	} elseif ( 'vietnamese' == $subset ) {
       
   194 		$subsets .= ',vietnamese';
       
   195 	}
       
   196 
       
   197 	if ( $fonts ) {
       
   198 		$fonts_url = add_query_arg( array(
       
   199 			'family' => urlencode( implode( '|', $fonts ) ),
       
   200 			'subset' => urlencode( $subsets ),
       
   201 		), '//fonts.googleapis.com/css' );
       
   202 	}
       
   203 
       
   204 	return $fonts_url;
       
   205 }
       
   206 endif;
       
   207 
       
   208 /**
       
   209  * JavaScript Detection.
       
   210  *
       
   211  * Adds a `js` class to the root `<html>` element when JavaScript is detected.
       
   212  *
       
   213  * @since Twenty Fifteen 1.1
       
   214  */
       
   215 function twentyfifteen_javascript_detection() {
       
   216 	echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
       
   217 }
       
   218 add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );
       
   219 
       
   220 /**
       
   221  * Enqueue scripts and styles.
       
   222  *
       
   223  * @since Twenty Fifteen 1.0
       
   224  */
       
   225 function twentyfifteen_scripts() {
       
   226 	// Add custom fonts, used in the main stylesheet.
       
   227 	wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
       
   228 
       
   229 	// Add Genericons, used in the main stylesheet.
       
   230 	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
       
   231 
       
   232 	// Load our main stylesheet.
       
   233 	wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
       
   234 
       
   235 	// Load the Internet Explorer specific stylesheet.
       
   236 	wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
       
   237 	wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
       
   238 
       
   239 	// Load the Internet Explorer 7 specific stylesheet.
       
   240 	wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
       
   241 	wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
       
   242 
       
   243 	wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
       
   244 
       
   245 	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
       
   246 		wp_enqueue_script( 'comment-reply' );
       
   247 	}
       
   248 
       
   249 	if ( is_singular() && wp_attachment_is_image() ) {
       
   250 		wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
       
   251 	}
       
   252 
       
   253 	wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150330', true );
       
   254 	wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
       
   255 		'expand'   => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
       
   256 		'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
       
   257 	) );
       
   258 }
       
   259 add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
       
   260 
       
   261 /**
       
   262  * Add featured image as background image to post navigation elements.
       
   263  *
       
   264  * @since Twenty Fifteen 1.0
       
   265  *
       
   266  * @see wp_add_inline_style()
       
   267  */
       
   268 function twentyfifteen_post_nav_background() {
       
   269 	if ( ! is_single() ) {
       
   270 		return;
       
   271 	}
       
   272 
       
   273 	$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
       
   274 	$next     = get_adjacent_post( false, '', false );
       
   275 	$css      = '';
       
   276 
       
   277 	if ( is_attachment() && 'attachment' == $previous->post_type ) {
       
   278 		return;
       
   279 	}
       
   280 
       
   281 	if ( $previous &&  has_post_thumbnail( $previous->ID ) ) {
       
   282 		$prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
       
   283 		$css .= '
       
   284 			.post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
       
   285 			.post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
       
   286 			.post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
       
   287 		';
       
   288 	}
       
   289 
       
   290 	if ( $next && has_post_thumbnail( $next->ID ) ) {
       
   291 		$nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
       
   292 		$css .= '
       
   293 			.post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); border-top: 0; }
       
   294 			.post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
       
   295 			.post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
       
   296 		';
       
   297 	}
       
   298 
       
   299 	wp_add_inline_style( 'twentyfifteen-style', $css );
       
   300 }
       
   301 add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
       
   302 
       
   303 /**
       
   304  * Display descriptions in main navigation.
       
   305  *
       
   306  * @since Twenty Fifteen 1.0
       
   307  *
       
   308  * @param string  $item_output The menu item output.
       
   309  * @param WP_Post $item        Menu item object.
       
   310  * @param int     $depth       Depth of the menu.
       
   311  * @param array   $args        wp_nav_menu() arguments.
       
   312  * @return string Menu item with possible description.
       
   313  */
       
   314 function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
       
   315 	if ( 'primary' == $args->theme_location && $item->description ) {
       
   316 		$item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
       
   317 	}
       
   318 
       
   319 	return $item_output;
       
   320 }
       
   321 add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
       
   322 
       
   323 /**
       
   324  * Add a `screen-reader-text` class to the search form's submit button.
       
   325  *
       
   326  * @since Twenty Fifteen 1.0
       
   327  *
       
   328  * @param string $html Search form HTML.
       
   329  * @return string Modified search form HTML.
       
   330  */
       
   331 function twentyfifteen_search_form_modify( $html ) {
       
   332 	return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
       
   333 }
       
   334 add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );
       
   335 
       
   336 /**
       
   337  * Implement the Custom Header feature.
       
   338  *
       
   339  * @since Twenty Fifteen 1.0
       
   340  */
       
   341 require get_template_directory() . '/inc/custom-header.php';
       
   342 
       
   343 /**
       
   344  * Custom template tags for this theme.
       
   345  *
       
   346  * @since Twenty Fifteen 1.0
       
   347  */
       
   348 require get_template_directory() . '/inc/template-tags.php';
       
   349 
       
   350 /**
       
   351  * Customizer additions.
       
   352  *
       
   353  * @since Twenty Fifteen 1.0
       
   354  */
       
   355 require get_template_directory() . '/inc/customizer.php';