3 * Twenty Fifteen functions and definitions
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.
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.
14 * @link https://codex.wordpress.org/Theme_Development
15 * @link https://codex.wordpress.org/Child_Themes
17 * Functions that are not pluggable (not wrapped in function_exists()) are
18 * instead attached to a filter or action hook.
20 * For more information on hooks, actions, and filters,
21 * {@link https://codex.wordpress.org/Plugin_API}
24 * @subpackage Twenty_Fifteen
25 * @since Twenty Fifteen 1.0
29 * Set the content width based on the theme's design and stylesheet.
31 * @since Twenty Fifteen 1.0
33 if ( ! isset( $content_width ) ) {
38 * Twenty Fifteen only works in WordPress 4.1 or later.
40 if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
41 require get_template_directory() . '/inc/back-compat.php';
44 if ( ! function_exists( 'twentyfifteen_setup' ) ) :
46 * Sets up theme defaults and registers support for various WordPress features.
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.
52 * @since Twenty Fifteen 1.0
54 function twentyfifteen_setup() {
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
62 load_theme_textdomain( 'twentyfifteen', get_template_directory() . '/languages' );
64 // Add default posts and comments RSS feed links to head.
65 add_theme_support( 'automatic-feed-links' );
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
73 add_theme_support( 'title-tag' );
76 * Enable support for Post Thumbnails on posts and pages.
78 * See: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
80 add_theme_support( 'post-thumbnails' );
81 set_post_thumbnail_size( 825, 510, true );
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' ),
90 * Switch default core markup for search form, comment form, and comments
91 * to output valid HTML5.
93 add_theme_support( 'html5', array(
94 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
98 * Enable support for Post Formats.
100 * See: https://codex.wordpress.org/Post_Formats
102 add_theme_support( 'post-formats', array(
103 'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
106 $color_scheme = twentyfifteen_get_color_scheme();
107 $default_color = trim( $color_scheme[0], '#' );
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',
116 * This theme styles the visual editor to resemble the theme style,
117 * specifically font, colors, icons, and column width.
119 add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
121 endif; // twentyfifteen_setup
122 add_action( 'after_setup_theme', 'twentyfifteen_setup' );
125 * Register widget area.
127 * @since Twenty Fifteen 1.0
129 * @link https://codex.wordpress.org/Function_Reference/register_sidebar
131 function twentyfifteen_widgets_init() {
132 register_sidebar( array(
133 'name' => __( 'Widget Area', 'twentyfifteen' ),
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>',
142 add_action( 'widgets_init', 'twentyfifteen_widgets_init' );
144 if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
146 * Register Google fonts for Twenty Fifteen.
148 * @since Twenty Fifteen 1.0
150 * @return string Google fonts URL for the theme.
152 function twentyfifteen_fonts_url() {
155 $subsets = 'latin,latin-ext';
157 /* translators: If there are characters in your language that are not supported by Noto Sans, translate this to 'off'. Do not translate into your own language. */
158 if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
159 $fonts[] = 'Noto Sans:400italic,700italic,400,700';
162 /* translators: If there are characters in your language that are not supported by Noto Serif, translate this to 'off'. Do not translate into your own language. */
163 if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
164 $fonts[] = 'Noto Serif:400italic,700italic,400,700';
167 /* translators: If there are characters in your language that are not supported by Inconsolata, translate this to 'off'. Do not translate into your own language. */
168 if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
169 $fonts[] = 'Inconsolata:400,700';
172 /* translators: To add an additional character subset specific to your language, translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language. */
173 $subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );
175 if ( 'cyrillic' == $subset ) {
176 $subsets .= ',cyrillic,cyrillic-ext';
177 } elseif ( 'greek' == $subset ) {
178 $subsets .= ',greek,greek-ext';
179 } elseif ( 'devanagari' == $subset ) {
180 $subsets .= ',devanagari';
181 } elseif ( 'vietnamese' == $subset ) {
182 $subsets .= ',vietnamese';
186 $fonts_url = add_query_arg( array(
187 'family' => urlencode( implode( '|', $fonts ) ),
188 'subset' => urlencode( $subsets ),
189 ), '//fonts.googleapis.com/css' );
197 * Enqueue scripts and styles.
199 * @since Twenty Fifteen 1.0
201 function twentyfifteen_scripts() {
202 // Add custom fonts, used in the main stylesheet.
203 wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );
205 // Add Genericons, used in the main stylesheet.
206 wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
208 // Load our main stylesheet.
209 wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );
211 // Load the Internet Explorer specific stylesheet.
212 wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
213 wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );
215 // Load the Internet Explorer 7 specific stylesheet.
216 wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
217 wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );
219 wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );
221 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
222 wp_enqueue_script( 'comment-reply' );
225 if ( is_singular() && wp_attachment_is_image() ) {
226 wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
229 wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20141212', true );
230 wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
231 'expand' => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
232 'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
235 add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );
238 * Add featured image as background image to post navigation elements.
240 * @since Twenty Fifteen 1.0
242 * @see wp_add_inline_style()
244 function twentyfifteen_post_nav_background() {
245 if ( ! is_single() ) {
249 $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
250 $next = get_adjacent_post( false, '', false );
253 if ( is_attachment() && 'attachment' == $previous->post_type ) {
257 if ( $previous && has_post_thumbnail( $previous->ID ) ) {
258 $prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
260 .post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
261 .post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
262 .post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
266 if ( $next && has_post_thumbnail( $next->ID ) ) {
267 $nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
269 .post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); }
270 .post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
271 .post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
275 wp_add_inline_style( 'twentyfifteen-style', $css );
277 add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );
280 * Display descriptions in main navigation.
282 * @since Twenty Fifteen 1.0
284 * @param string $item_output The menu item output.
285 * @param WP_Post $item Menu item object.
286 * @param int $depth Depth of the menu.
287 * @param array $args wp_nav_menu() arguments.
288 * @return string Menu item with possible description.
290 function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
291 if ( 'primary' == $args->theme_location && $item->description ) {
292 $item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
297 add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );
300 * Add a `screen-reader-text` class to the search form's submit button.
302 * @since Twenty Fifteen 1.0
304 * @param string $html Search form HTML.
305 * @return string Modified search form HTML.
307 function twentyfifteen_search_form_modify( $html ) {
308 return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
310 add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );
313 * Implement the Custom Header feature.
315 * @since Twenty Fifteen 1.0
317 require get_template_directory() . '/inc/custom-header.php';
320 * Custom template tags for this theme.
322 * @since Twenty Fifteen 1.0
324 require get_template_directory() . '/inc/template-tags.php';
327 * Customizer additions.
329 * @since Twenty Fifteen 1.0
331 require get_template_directory() . '/inc/customizer.php';