| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Functions which enhance the theme by hooking into WordPress 4 * 5 * @package WordPress 6 * @subpackage Twenty_Twenty_One 7 * @since Twenty Twenty-One 1.0 8 */ 9 10 /** 11 * Adds custom classes to the array of body classes. 12 * 13 * @since Twenty Twenty-One 1.0 14 * 15 * @param array $classes Classes for the body element. 16 * @return string[] Body classes with theme-specific additions. 17 */ 18 function twenty_twenty_one_body_classes( $classes ) { 19 20 // Helps detect if JS is enabled or not. 21 $classes[] = 'no-js'; 22 23 // Adds `singular` to singular pages, and `hfeed` to all other pages. 24 $classes[] = is_singular() ? 'singular' : 'hfeed'; 25 26 // Add a body class if main navigation is active. 27 if ( has_nav_menu( 'primary' ) ) { 28 $classes[] = 'has-main-navigation'; 29 } 30 31 // Add a body class if there are no footer widgets. 32 if ( ! is_active_sidebar( 'sidebar-1' ) ) { 33 $classes[] = 'no-widgets'; 34 } 35 36 return $classes; 37 } 38 add_filter( 'body_class', 'twenty_twenty_one_body_classes' ); 39 40 /** 41 * Adds custom class to the array of posts classes. 42 * 43 * @since Twenty Twenty-One 1.0 44 * 45 * @param array $classes An array of CSS classes. 46 * @return string[] Post classes with 'entry' class added. 47 */ 48 function twenty_twenty_one_post_classes( $classes ) { 49 $classes[] = 'entry'; 50 51 return $classes; 52 } 53 add_filter( 'post_class', 'twenty_twenty_one_post_classes', 10, 3 ); 54 55 /** 56 * Adds a pingback url auto-discovery header for single posts, pages, or attachments. 57 * 58 * @since Twenty Twenty-One 1.0 59 * 60 * @return void 61 */ 62 function twenty_twenty_one_pingback_header() { 63 if ( is_singular() && pings_open() ) { 64 echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">'; 65 } 66 } 67 add_action( 'wp_head', 'twenty_twenty_one_pingback_header' ); 68 69 /** 70 * Removes the `no-js` class from body if JS is supported. 71 * 72 * @since Twenty Twenty-One 1.0 73 * 74 * @return void 75 */ 76 function twenty_twenty_one_supports_js() { 77 $js = "document.body.classList.remove('no-js');"; 78 $js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ ); 79 80 if ( function_exists( 'wp_print_inline_script_tag' ) ) { 81 wp_print_inline_script_tag( $js ); 82 } else { 83 echo "<script>$js</script>\n"; 84 } 85 } 86 add_action( 'wp_footer', 'twenty_twenty_one_supports_js' ); 87 88 /** 89 * Changes comment form default fields. 90 * 91 * @since Twenty Twenty-One 1.0 92 * 93 * @param array $defaults The form defaults. 94 * @return array Comment form defaults with adjusted textarea height. 95 */ 96 function twenty_twenty_one_comment_form_defaults( $defaults ) { 97 98 // Adjust height of comment form. 99 $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $defaults['comment_field'] ); 100 101 return $defaults; 102 } 103 add_filter( 'comment_form_defaults', 'twenty_twenty_one_comment_form_defaults' ); 104 105 /** 106 * Determines if post thumbnail can be displayed. 107 * 108 * @since Twenty Twenty-One 1.0 109 * 110 * @return bool 111 */ 112 function twenty_twenty_one_can_show_post_thumbnail() { 113 /** 114 * Filters whether post thumbnail can be displayed. 115 * 116 * @since Twenty Twenty-One 1.0 117 * 118 * @param bool $show_post_thumbnail Whether to show post thumbnail. 119 */ 120 return apply_filters( 121 'twenty_twenty_one_can_show_post_thumbnail', 122 ! post_password_required() && ! is_attachment() && has_post_thumbnail() 123 ); 124 } 125 126 /** 127 * Returns the size for avatars used in the theme. 128 * 129 * @since Twenty Twenty-One 1.0 130 * 131 * @return int 132 */ 133 function twenty_twenty_one_get_avatar_size() { 134 return 60; 135 } 136 137 /** 138 * Creates continue reading text. 139 * 140 * @since Twenty Twenty-One 1.0 141 */ 142 function twenty_twenty_one_continue_reading_text() { 143 $continue_reading = sprintf( 144 /* translators: %s: Post title. Only visible to screen readers. */ 145 esc_html__( 'Continue reading %s', 'twentytwentyone' ), 146 the_title( '<span class="screen-reader-text">', '</span>', false ) 147 ); 148 149 return $continue_reading; 150 } 151 152 /** 153 * Creates the continue reading link for excerpt. 154 * 155 * @since Twenty Twenty-One 1.0 156 */ 157 function twenty_twenty_one_continue_reading_link_excerpt() { 158 if ( ! is_admin() ) { 159 return '… <a class="more-link" href="' . esc_url( get_permalink() ) . '">' . twenty_twenty_one_continue_reading_text() . '</a>'; 160 } 161 } 162 163 // Filter the excerpt more link. 164 add_filter( 'excerpt_more', 'twenty_twenty_one_continue_reading_link_excerpt' ); 165 166 /** 167 * Creates the continue reading link. 168 * 169 * @since Twenty Twenty-One 1.0 170 */ 171 function twenty_twenty_one_continue_reading_link() { 172 if ( ! is_admin() ) { 173 return '<div class="more-link-container"><a class="more-link" href="' . esc_url( get_permalink() ) . '#more-' . esc_attr( get_the_ID() ) . '">' . twenty_twenty_one_continue_reading_text() . '</a></div>'; 174 } 175 } 176 177 // Filter the content more link. 178 add_filter( 'the_content_more_link', 'twenty_twenty_one_continue_reading_link' ); 179 180 if ( ! function_exists( 'twenty_twenty_one_post_title' ) ) { 181 /** 182 * Adds a title to posts and pages that are missing titles. 183 * 184 * @since Twenty Twenty-One 1.0 185 * @since Twenty Twenty-One 2.9 Only applies the filter on the front end. 186 * 187 * @param string $title The title. 188 * @return string 189 */ 190 function twenty_twenty_one_post_title( $title ) { 191 if ( is_admin() ) { 192 return $title; 193 } 194 195 return '' === $title ? esc_html_x( 'Untitled', 'Added on the front end to posts and pages that are missing titles', 'twentytwentyone' ) : $title; 196 } 197 } 198 add_filter( 'the_title', 'twenty_twenty_one_post_title' ); 199 200 /** 201 * Gets the SVG code for a given icon. 202 * 203 * @since Twenty Twenty-One 1.0 204 * 205 * @param string $group The icon group. 206 * @param string $icon The icon. 207 * @param int $size The icon size in pixels. 208 * @return string SVG code for the requested icon. 209 */ 210 function twenty_twenty_one_get_icon_svg( $group, $icon, $size = 24 ) { 211 return Twenty_Twenty_One_SVG_Icons::get_svg( $group, $icon, $size ); 212 } 213 214 /** 215 * Changes the default navigation arrows to svg icons 216 * 217 * @since Twenty Twenty-One 1.0 218 * 219 * @param string $calendar_output The generated HTML of the calendar. 220 * @return string Calendar HTML with SVG navigation arrows. 221 */ 222 function twenty_twenty_one_change_calendar_nav_arrows( $calendar_output ) { 223 $calendar_output = str_replace( '« ', is_rtl() ? twenty_twenty_one_get_icon_svg( 'ui', 'arrow_right' ) : twenty_twenty_one_get_icon_svg( 'ui', 'arrow_left' ), $calendar_output ); 224 $calendar_output = str_replace( ' »', is_rtl() ? twenty_twenty_one_get_icon_svg( 'ui', 'arrow_left' ) : twenty_twenty_one_get_icon_svg( 'ui', 'arrow_right' ), $calendar_output ); 225 return $calendar_output; 226 } 227 add_filter( 'get_calendar', 'twenty_twenty_one_change_calendar_nav_arrows' ); 228 229 /** 230 * Gets custom CSS. 231 * 232 * Return CSS for non-latin language, if available, or null 233 * 234 * @since Twenty Twenty-One 1.0 235 * 236 * @param string $type Whether to return CSS for the "front-end", "block-editor", or "classic-editor". 237 * @return string CSS styles for non-Latin languages based on the site locale. 238 */ 239 function twenty_twenty_one_get_non_latin_css( $type = 'front-end' ) { 240 241 // Fetch site locale. 242 $locale = get_bloginfo( 'language' ); 243 244 /** 245 * Filters the fallback fonts for non-latin languages. 246 * 247 * @since Twenty Twenty-One 1.0 248 * 249 * @param array $font_family An array of locales and font families. 250 */ 251 $font_family = apply_filters( 252 'twenty_twenty_one_get_localized_font_family_types', 253 array( 254 255 // Arabic. 256 'ar' => array( 'Tahoma', 'Arial', 'sans-serif' ), 257 'ary' => array( 'Tahoma', 'Arial', 'sans-serif' ), 258 'azb' => array( 'Tahoma', 'Arial', 'sans-serif' ), 259 'ckb' => array( 'Tahoma', 'Arial', 'sans-serif' ), 260 'fa-IR' => array( 'Tahoma', 'Arial', 'sans-serif' ), 261 'haz' => array( 'Tahoma', 'Arial', 'sans-serif' ), 262 'ps' => array( 'Tahoma', 'Arial', 'sans-serif' ), 263 264 // Chinese Simplified (China) - Noto Sans SC. 265 'zh-CN' => array( '\'PingFang SC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ), 266 267 // Chinese Traditional (Taiwan) - Noto Sans TC. 268 'zh-TW' => array( '\'PingFang TC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ), 269 270 // Chinese (Hong Kong) - Noto Sans HK. 271 'zh-HK' => array( '\'PingFang HK\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ), 272 273 // Cyrillic. 274 'bel' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 275 'bg-BG' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 276 'kk' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 277 'mk-MK' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 278 'mn' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 279 'ru-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 280 'sah' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 281 'sr-RS' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 282 'tt-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 283 'uk' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ), 284 285 // Devanagari. 286 'bn-BD' => array( 'Arial', 'sans-serif' ), 287 'hi-IN' => array( 'Arial', 'sans-serif' ), 288 'mr' => array( 'Arial', 'sans-serif' ), 289 'ne-NP' => array( 'Arial', 'sans-serif' ), 290 291 // Greek. 292 'el' => array( '\'Helvetica Neue\', Helvetica, Arial, sans-serif' ), 293 294 // Gujarati. 295 'gu' => array( 'Arial', 'sans-serif' ), 296 297 // Hebrew. 298 'he-IL' => array( '\'Arial Hebrew\'', 'Arial', 'sans-serif' ), 299 300 // Japanese. 301 'ja' => array( 'sans-serif' ), 302 303 // Korean. 304 'ko-KR' => array( '\'Apple SD Gothic Neo\'', '\'Malgun Gothic\'', '\'Nanum Gothic\'', 'Dotum', 'sans-serif' ), 305 306 // Thai. 307 'th' => array( '\'Sukhumvit Set\'', '\'Helvetica Neue\'', 'Helvetica', 'Arial', 'sans-serif' ), 308 309 // Vietnamese. 310 'vi' => array( '\'Libre Franklin\'', 'sans-serif' ), 311 312 ) 313 ); 314 315 // Return if the selected language has no fallback fonts. 316 if ( empty( $font_family[ $locale ] ) ) { 317 return ''; 318 } 319 320 /** 321 * Filters the elements to apply fallback fonts to. 322 * 323 * @since Twenty Twenty-One 1.0 324 * 325 * @param array $elements An array of elements for "front-end", "block-editor", or "classic-editor". 326 */ 327 $elements = apply_filters( 328 'twenty_twenty_one_get_localized_font_family_elements', 329 array( 330 'front-end' => array( 'body', 'input', 'textarea', 'button', '.button', '.faux-button', '.wp-block-button__link', '.wp-block-file__button', '.has-drop-cap:not(:focus)::first-letter', '.entry-content .wp-block-archives', '.entry-content .wp-block-categories', '.entry-content .wp-block-cover-image', '.entry-content .wp-block-latest-comments', '.entry-content .wp-block-latest-posts', '.entry-content .wp-block-pullquote', '.entry-content .wp-block-quote.is-large', '.entry-content .wp-block-quote.is-style-large', '.entry-content .wp-block-archives *', '.entry-content .wp-block-categories *', '.entry-content .wp-block-latest-posts *', '.entry-content .wp-block-latest-comments *', '.entry-content p', '.entry-content ol', '.entry-content ul', '.entry-content dl', '.entry-content dt', '.entry-content cite', '.entry-content figcaption', '.entry-content .wp-caption-text', '.comment-content p', '.comment-content ol', '.comment-content ul', '.comment-content dl', '.comment-content dt', '.comment-content cite', '.comment-content figcaption', '.comment-content .wp-caption-text', '.widget_text p', '.widget_text ol', '.widget_text ul', '.widget_text dl', '.widget_text dt', '.widget-content .rssSummary', '.widget-content cite', '.widget-content figcaption', '.widget-content .wp-caption-text' ), 331 'block-editor' => array( '.editor-styles-wrapper > *', '.editor-styles-wrapper p', '.editor-styles-wrapper ol', '.editor-styles-wrapper ul', '.editor-styles-wrapper dl', '.editor-styles-wrapper dt', '.editor-post-title__block .editor-post-title__input', '.editor-styles-wrapper .wp-block h1', '.editor-styles-wrapper .wp-block h2', '.editor-styles-wrapper .wp-block h3', '.editor-styles-wrapper .wp-block h4', '.editor-styles-wrapper .wp-block h5', '.editor-styles-wrapper .wp-block h6', '.editor-styles-wrapper .has-drop-cap:not(:focus)::first-letter', '.editor-styles-wrapper cite', '.editor-styles-wrapper figcaption', '.editor-styles-wrapper .wp-caption-text' ), 332 'classic-editor' => array( 'body#tinymce.wp-editor', 'body#tinymce.wp-editor p', 'body#tinymce.wp-editor ol', 'body#tinymce.wp-editor ul', 'body#tinymce.wp-editor dl', 'body#tinymce.wp-editor dt', 'body#tinymce.wp-editor figcaption', 'body#tinymce.wp-editor .wp-caption-text', 'body#tinymce.wp-editor .wp-caption-dd', 'body#tinymce.wp-editor cite', 'body#tinymce.wp-editor table' ), 333 ) 334 ); 335 336 // Return if the specified type doesn't exist. 337 if ( empty( $elements[ $type ] ) ) { 338 return ''; 339 } 340 341 // Include file if function doesn't exist. 342 if ( ! function_exists( 'twenty_twenty_one_generate_css' ) ) { 343 require_once get_theme_file_path( 'inc/custom-css.php' ); 344 } 345 346 // Return the specified styles. 347 return twenty_twenty_one_generate_css( 348 implode( ',', $elements[ $type ] ), 349 'font-family', 350 implode( ',', $font_family[ $locale ] ), 351 '', 352 '', 353 false 354 ); 355 } 356 357 /** 358 * Prints the first instance of a block in the content, and then break away. 359 * 360 * @since Twenty Twenty-One 1.0 361 * 362 * @param string $block_name The full block type name, or a partial match. 363 * Example: `core/image`, `core-embed/*`. 364 * @param string|null $content The content to search in. Use null for get_the_content(). 365 * @param int $instances How many instances of the block will be printed (max). Default 1. 366 * @return bool Returns true if a block was located & printed, otherwise false. 367 */ 368 function twenty_twenty_one_print_first_instance_of_block( $block_name, $content = null, $instances = 1 ) { 369 $instances_count = 0; 370 $blocks_content = ''; 371 372 if ( ! $content ) { 373 $content = get_the_content(); 374 } 375 376 // Parse blocks in the content. 377 $blocks = parse_blocks( $content ); 378 379 // Loop blocks. 380 foreach ( $blocks as $block ) { 381 382 // Confidence check. 383 if ( ! isset( $block['blockName'] ) ) { 384 continue; 385 } 386 387 // Check if this the block matches the $block_name. 388 $is_matching_block = false; 389 390 // If the block ends with *, try to match the first portion. 391 if ( '*' === $block_name[-1] ) { 392 $is_matching_block = 0 === strpos( $block['blockName'], rtrim( $block_name, '*' ) ); 393 } else { 394 $is_matching_block = $block_name === $block['blockName']; 395 } 396 397 if ( $is_matching_block ) { 398 // Increment count. 399 ++$instances_count; 400 401 // Add the block HTML. 402 $blocks_content .= render_block( $block ); 403 404 // Break the loop if the $instances count was reached. 405 if ( $instances_count >= $instances ) { 406 break; 407 } 408 } 409 } 410 411 if ( $blocks_content ) { 412 /** This filter is documented in wp-includes/post-template.php */ 413 echo apply_filters( 'the_content', $blocks_content ); 414 return true; 415 } 416 417 return false; 418 } 419 420 /** 421 * Retrieves protected post password form content. 422 * 423 * @since Twenty Twenty-One 1.0 424 * @since Twenty Twenty-One 1.4 Corrected parameter name for `$output`, 425 * added the `$post` parameter. 426 * 427 * @param string $output The password form HTML output. 428 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 429 * @return string HTML content for password form for password protected post. 430 */ 431 function twenty_twenty_one_password_form( $output, $post = 0 ) { 432 $post = get_post( $post ); 433 $label = 'pwbox-' . ( empty( $post->ID ) ? wp_rand() : $post->ID ); 434 $output = '<p class="post-password-message">' . esc_html__( 'This content is password protected. Please enter a password to view.', 'twentytwentyone' ) . '</p> 435 <form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post"> 436 <label class="post-password-form__label" for="' . esc_attr( $label ) . '">' . esc_html_x( 'Password', 'Post password form', 'twentytwentyone' ) . '</label><input class="post-password-form__input" name="post_password" id="' . esc_attr( $label ) . '" type="password" spellcheck="false" size="20" /><input type="submit" class="post-password-form__submit" name="' . esc_attr_x( 'Submit', 'Post password form', 'twentytwentyone' ) . '" value="' . esc_attr_x( 'Enter', 'Post password form', 'twentytwentyone' ) . '" /></form> 437 '; 438 return $output; 439 } 440 add_filter( 'the_password_form', 'twenty_twenty_one_password_form', 10, 2 ); 441 442 /** 443 * Filters the list of attachment image attributes. 444 * 445 * @since Twenty Twenty-One 1.0 446 * 447 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. 448 * See wp_get_attachment_image(). 449 * @param WP_Post $attachment Image attachment post. 450 * @param string|int[] $size Requested image size. Can be any registered image size name, or 451 * an array of width and height values in pixels (in that order). 452 * @return string[] The filtered attributes for the image markup. 453 */ 454 function twenty_twenty_one_get_attachment_image_attributes( $attr, $attachment, $size ) { 455 456 if ( is_admin() ) { 457 return $attr; 458 } 459 460 if ( isset( $attr['class'] ) && false !== strpos( $attr['class'], 'custom-logo' ) ) { 461 return $attr; 462 } 463 464 $width = false; 465 $height = false; 466 467 if ( is_array( $size ) ) { 468 $width = (int) $size[0]; 469 $height = (int) $size[1]; 470 } elseif ( $attachment && is_object( $attachment ) && $attachment->ID ) { 471 $meta = wp_get_attachment_metadata( $attachment->ID ); 472 if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) { 473 $width = (int) $meta['width']; 474 $height = (int) $meta['height']; 475 } 476 } 477 478 if ( $width && $height ) { 479 480 // Add style. 481 $attr['style'] = isset( $attr['style'] ) ? $attr['style'] : ''; 482 $attr['style'] = 'width:100%;height:' . round( 100 * $height / $width, 2 ) . '%;max-width:' . $width . 'px;' . $attr['style']; 483 } 484 485 return $attr; 486 } 487 add_filter( 'wp_get_attachment_image_attributes', 'twenty_twenty_one_get_attachment_image_attributes', 10, 3 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Aug 8 08:20:21 2026 | Cross-referenced by PHPXref |