[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Twenty Twelve functions and definitions
   4   *
   5   * Sets up the theme and provides some helper functions, which are used
   6   * in the theme as custom template tags. Others are attached to action and
   7   * filter 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://developer.wordpress.org/themes/basics/theme-functions/
  15   * @link https://developer.wordpress.org/themes/advanced-topics/child-themes/
  16   *
  17   * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
  18   * to a filter or action hook.
  19   *
  20   * For more information on hooks, actions, and filters, @link https://developer.wordpress.org/plugins/
  21   *
  22   * @package WordPress
  23   * @subpackage Twenty_Twelve
  24   * @since Twenty Twelve 1.0
  25   */
  26  
  27  // Set up the content width value based on the theme's design and stylesheet.
  28  if ( ! isset( $content_width ) ) {
  29      $content_width = 625;
  30  }
  31  
  32  /**
  33   * Twenty Twelve setup.
  34   *
  35   * Sets up theme defaults and registers the various WordPress features that
  36   * Twenty Twelve supports.
  37   *
  38   * @uses load_theme_textdomain() For translation/localization support.
  39   * @uses add_editor_style() To add a Visual Editor stylesheet.
  40   * @uses add_theme_support() To add support for post thumbnails, automatic feed links,
  41   *  custom background, and post formats.
  42   * @uses register_nav_menu() To add support for navigation menus.
  43   * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
  44   *
  45   * @since Twenty Twelve 1.0
  46   *
  47   * @global string $wp_version The WordPress version string.
  48   */
  49  function twentytwelve_setup() {
  50      /*
  51       * Makes Twenty Twelve available for translation.
  52       *
  53       * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/twentytwelve
  54       * If you're building a theme based on Twenty Twelve, use a find and replace
  55       * to change 'twentytwelve' to the name of your theme in all the template files.
  56       *
  57       * Manual loading of text domain is not required after the introduction of
  58       * just in time translation loading in WordPress version 4.6.
  59       *
  60       * @ticket 58318
  61       */
  62      if ( version_compare( $GLOBALS['wp_version'], '4.6', '<' ) ) {
  63          load_theme_textdomain( 'twentytwelve' );
  64      }
  65  
  66      // This theme styles the visual editor with editor-style.css to match the theme style.
  67      add_editor_style();
  68  
  69      // Load regular editor styles into the new block-based editor.
  70      add_theme_support( 'editor-styles' );
  71  
  72      // Load default block styles.
  73      add_theme_support( 'wp-block-styles' );
  74  
  75      // Add support for responsive embeds.
  76      add_theme_support( 'responsive-embeds' );
  77  
  78      // Add support for custom color scheme.
  79      add_theme_support(
  80          'editor-color-palette',
  81          array(
  82              array(
  83                  'name'  => __( 'Blue', 'twentytwelve' ),
  84                  'slug'  => 'blue',
  85                  'color' => '#21759b',
  86              ),
  87              array(
  88                  'name'  => __( 'Dark Gray', 'twentytwelve' ),
  89                  'slug'  => 'dark-gray',
  90                  'color' => '#444',
  91              ),
  92              array(
  93                  'name'  => __( 'Medium Gray', 'twentytwelve' ),
  94                  'slug'  => 'medium-gray',
  95                  'color' => '#9f9f9f',
  96              ),
  97              array(
  98                  'name'  => __( 'Light Gray', 'twentytwelve' ),
  99                  'slug'  => 'light-gray',
 100                  'color' => '#e6e6e6',
 101              ),
 102              array(
 103                  'name'  => __( 'White', 'twentytwelve' ),
 104                  'slug'  => 'white',
 105                  'color' => '#fff',
 106              ),
 107          )
 108      );
 109  
 110      // Adds RSS feed links to <head> for posts and comments.
 111      add_theme_support( 'automatic-feed-links' );
 112  
 113      // This theme supports a variety of post formats.
 114      add_theme_support( 'post-formats', array( 'aside', 'image', 'link', 'quote', 'status' ) );
 115  
 116      // This theme uses wp_nav_menu() in one location.
 117      register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ) );
 118  
 119      /*
 120       * This theme supports custom background color and image,
 121       * and here we also set up the default background color.
 122       */
 123      add_theme_support(
 124          'custom-background',
 125          array(
 126              'default-color' => 'e6e6e6',
 127          )
 128      );
 129  
 130      // This theme uses a custom image size for featured images, displayed on "standard" posts.
 131      add_theme_support( 'post-thumbnails' );
 132      set_post_thumbnail_size( 624, 9999 ); // Unlimited height, soft crop.
 133  
 134      // Indicate widget sidebars can use selective refresh in the Customizer.
 135      add_theme_support( 'customize-selective-refresh-widgets' );
 136  }
 137  add_action( 'after_setup_theme', 'twentytwelve_setup' );
 138  
 139  /**
 140   * Adds support for a custom header image.
 141   */
 142  require get_template_directory() . '/inc/custom-header.php';
 143  
 144  /**
 145   * Registers block patterns and pattern categories.
 146   *
 147   * @since Twenty Twelve 4.4
 148   */
 149  function twentytwelve_register_block_patterns() {
 150      require get_template_directory() . '/inc/block-patterns.php';
 151  }
 152  
 153  add_action( 'init', 'twentytwelve_register_block_patterns' );
 154  
 155  if ( ! function_exists( 'twentytwelve_get_font_url' ) ) :
 156      /**
 157       * Return the font stylesheet URL if available.
 158       *
 159       * The use of Open Sans by default is localized. For languages that use
 160       * characters not supported by the font, the font can be disabled.
 161       *
 162       * @since Twenty Twelve 1.2
 163       * @since Twenty Twelve 3.9 Replaced Google URL with self-hosted font.
 164       *
 165       * @return string Font stylesheet URL or empty string if disabled.
 166       */
 167  	function twentytwelve_get_font_url() {
 168          $font_url = '';
 169  
 170          /*
 171          * translators: If there are characters in your language that are not supported
 172          * by Open Sans, translate this to 'off'. Do not translate into your own language.
 173          */
 174          if ( 'off' !== _x( 'on', 'Open Sans font: on or off', 'twentytwelve' ) ) {
 175              $font_url = get_template_directory_uri() . '/fonts/font-open-sans.css';
 176          }
 177  
 178          return $font_url;
 179      }
 180  endif;
 181  
 182  /**
 183   * Enqueues scripts and styles for front end.
 184   *
 185   * @since Twenty Twelve 1.0
 186   *
 187   * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
 188   */
 189  function twentytwelve_scripts_styles() {
 190      global $wp_styles;
 191  
 192      /*
 193       * Adds JavaScript to pages with the comment form to support
 194       * sites with threaded comments (when in use).
 195       */
 196      if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
 197          wp_enqueue_script( 'comment-reply' );
 198      }
 199  
 200      // Adds JavaScript for handling the navigation menu hide-and-show behavior.
 201      wp_enqueue_script(
 202          'twentytwelve-navigation',
 203          get_template_directory_uri() . '/js/navigation.js',
 204          array( 'jquery' ),
 205          '20250303',
 206          array(
 207              'in_footer' => false, // Because involves header.
 208              'strategy'  => 'defer',
 209          )
 210      );
 211  
 212      $font_url = twentytwelve_get_font_url();
 213      if ( ! empty( $font_url ) ) {
 214          $font_version = ( 0 === strpos( (string) twentytwelve_get_font_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null;
 215          wp_enqueue_style( 'twentytwelve-fonts', esc_url_raw( $font_url ), array(), $font_version );
 216      }
 217  
 218      // Loads our main stylesheet.
 219      wp_enqueue_style( 'twentytwelve-style', get_stylesheet_uri(), array(), '20260520' );
 220  
 221      // Theme block stylesheet.
 222      wp_enqueue_style( 'twentytwelve-block-style', get_template_directory_uri() . '/css/blocks.css', array( 'twentytwelve-style' ), '20251031' );
 223  
 224      // Register the Internet Explorer specific stylesheet.
 225      wp_register_style( 'twentytwelve-ie', false, array( 'twentytwelve-style' ) );
 226  }
 227  add_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' );
 228  
 229  /**
 230   * Enqueues styles for the block-based editor.
 231   *
 232   * @since Twenty Twelve 2.6
 233   */
 234  function twentytwelve_block_editor_styles() {
 235      // Block styles.
 236      wp_enqueue_style( 'twentytwelve-block-editor-style', get_template_directory_uri() . '/css/editor-blocks.css', array(), '20260324' );
 237      // Add custom fonts.
 238      $font_version = ( 0 === strpos( (string) twentytwelve_get_font_url(), get_template_directory_uri() . '/' ) ) ? '20230328' : null;
 239      wp_enqueue_style( 'twentytwelve-fonts', twentytwelve_get_font_url(), array(), $font_version );
 240  }
 241  add_action( 'enqueue_block_editor_assets', 'twentytwelve_block_editor_styles' );
 242  
 243  /**
 244   * Adds preconnect for Google Fonts.
 245   *
 246   * @since Twenty Twelve 2.2
 247   * @deprecated Twenty Twelve 3.9 Disabled filter because, by default, fonts are self-hosted.
 248   *
 249   * @global string $wp_version The WordPress version string.
 250   *
 251   * @param array   $urls          URLs to print for resource hints.
 252   * @param string  $relation_type The relation type the URLs are printed.
 253   * @return array URLs to print for resource hints.
 254   */
 255  function twentytwelve_resource_hints( $urls, $relation_type ) {
 256      if ( wp_style_is( 'twentytwelve-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
 257          if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '>=' ) ) {
 258              $urls[] = array(
 259                  'href' => 'https://fonts.gstatic.com',
 260                  'crossorigin',
 261              );
 262          } else {
 263              $urls[] = 'https://fonts.gstatic.com';
 264          }
 265      }
 266  
 267      return $urls;
 268  }
 269  // add_filter( 'wp_resource_hints', 'twentytwelve_resource_hints', 10, 2 );
 270  
 271  /**
 272   * Filters TinyMCE CSS path to include hosted fonts.
 273   *
 274   * Adds additional stylesheets to the TinyMCE editor if needed.
 275   *
 276   * @uses twentytwelve_get_font_url() To get the font stylesheet URL.
 277   *
 278   * @since Twenty Twelve 1.2
 279   *
 280   * @param string $mce_css CSS path to load in TinyMCE.
 281   * @return string Filtered CSS path.
 282   */
 283  function twentytwelve_mce_css( $mce_css ) {
 284      $font_url = twentytwelve_get_font_url();
 285  
 286      if ( empty( $font_url ) ) {
 287          return $mce_css;
 288      }
 289  
 290      if ( ! empty( $mce_css ) ) {
 291          $mce_css .= ',';
 292      }
 293  
 294      $mce_css .= esc_url_raw( str_replace( ',', '%2C', $font_url ) );
 295  
 296      return $mce_css;
 297  }
 298  add_filter( 'mce_css', 'twentytwelve_mce_css' );
 299  
 300  /**
 301   * Filters the page title.
 302   *
 303   * Creates a nicely formatted and more specific title element text
 304   * for output in head of document, based on current view.
 305   *
 306   * @since Twenty Twelve 1.0
 307   *
 308   * @global int $paged Page number of a list of posts.
 309   * @global int $page  Page number of a single post.
 310   *
 311   * @param string $title Default title text for current view.
 312   * @param string $sep Optional separator.
 313   * @return string Filtered title.
 314   */
 315  function twentytwelve_wp_title( $title, $sep ) {
 316      global $paged, $page;
 317  
 318      if ( is_feed() ) {
 319          return $title;
 320      }
 321  
 322      // Add the site name.
 323      $title .= get_bloginfo( 'name', 'display' );
 324  
 325      // Add the site description for the home/front page.
 326      $site_description = get_bloginfo( 'description', 'display' );
 327      if ( $site_description && ( is_home() || is_front_page() ) ) {
 328          $title = "$title $sep $site_description";
 329      }
 330  
 331      // Add a page number if necessary.
 332      if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
 333          /* translators: %s: Page number. */
 334          $title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );
 335      }
 336  
 337      return $title;
 338  }
 339  add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
 340  
 341  /**
 342   * Filters the page menu arguments.
 343   *
 344   * Makes our wp_nav_menu() fallback -- wp_page_menu() -- show a home link.
 345   *
 346   * @since Twenty Twelve 1.0
 347   */
 348  function twentytwelve_page_menu_args( $args ) {
 349      if ( ! isset( $args['show_home'] ) ) {
 350          $args['show_home'] = true;
 351      }
 352      return $args;
 353  }
 354  add_filter( 'wp_page_menu_args', 'twentytwelve_page_menu_args' );
 355  
 356  /**
 357   * Registers sidebars.
 358   *
 359   * Registers our main widget area and the front page widget areas.
 360   *
 361   * @since Twenty Twelve 1.0
 362   */
 363  function twentytwelve_widgets_init() {
 364      register_sidebar(
 365          array(
 366              'name'          => __( 'Main Sidebar', 'twentytwelve' ),
 367              'id'            => 'sidebar-1',
 368              'description'   => __( 'Appears on posts and pages except the optional Front Page template, which has its own widgets', 'twentytwelve' ),
 369              'before_widget' => '<aside id="%1$s" class="widget %2$s">',
 370              'after_widget'  => '</aside>',
 371              'before_title'  => '<h3 class="widget-title">',
 372              'after_title'   => '</h3>',
 373          )
 374      );
 375  
 376      register_sidebar(
 377          array(
 378              'name'          => __( 'First Front Page Widget Area', 'twentytwelve' ),
 379              'id'            => 'sidebar-2',
 380              'description'   => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
 381              'before_widget' => '<aside id="%1$s" class="widget %2$s">',
 382              'after_widget'  => '</aside>',
 383              'before_title'  => '<h3 class="widget-title">',
 384              'after_title'   => '</h3>',
 385          )
 386      );
 387  
 388      register_sidebar(
 389          array(
 390              'name'          => __( 'Second Front Page Widget Area', 'twentytwelve' ),
 391              'id'            => 'sidebar-3',
 392              'description'   => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
 393              'before_widget' => '<aside id="%1$s" class="widget %2$s">',
 394              'after_widget'  => '</aside>',
 395              'before_title'  => '<h3 class="widget-title">',
 396              'after_title'   => '</h3>',
 397          )
 398      );
 399  }
 400  add_action( 'widgets_init', 'twentytwelve_widgets_init' );
 401  
 402  if ( ! function_exists( 'wp_get_list_item_separator' ) ) :
 403      /**
 404       * Retrieves the list item separator based on the locale.
 405       *
 406       * Added for backward compatibility to support pre-6.0.0 WordPress versions.
 407       *
 408       * @since Twenty Twelve 3.7
 409       *
 410       * @return string Locale-specific list item separator.
 411       */
 412  	function wp_get_list_item_separator() {
 413          /* translators: Used between list items, there is a space after the comma. */
 414          return __( ', ', 'twentytwelve' );
 415      }
 416  endif;
 417  
 418  if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
 419      /**
 420       * Displays navigation to next/previous pages when applicable.
 421       *
 422       * @since Twenty Twelve 1.0
 423       *
 424       * @global WP_Query $wp_query WordPress Query object.
 425       */
 426  	function twentytwelve_content_nav( $html_id ) {
 427          global $wp_query;
 428  
 429          if ( $wp_query->max_num_pages > 1 ) :
 430              $order   = get_query_var( 'order', 'DESC' );
 431              $is_desc = ( 'DESC' === $order );
 432  
 433              $new_posts_text = __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentytwelve' );
 434              $old_posts_text = __( '<span class="meta-nav">&larr;</span> Older posts', 'twentytwelve' );
 435  
 436              $prev_link = $is_desc ? get_next_posts_link( $old_posts_text ) : get_previous_posts_link( $old_posts_text );
 437              $next_link = $is_desc ? get_previous_posts_link( $new_posts_text ) : get_next_posts_link( $new_posts_text );
 438              ?>
 439              <nav id="<?php echo esc_attr( $html_id ); ?>" class="navigation">
 440                  <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
 441                  <?php if ( $prev_link ) : ?>
 442                      <div class="nav-previous"><?php echo $prev_link; ?></div>
 443                  <?php endif; ?>
 444  
 445                  <?php if ( $next_link ) : ?>
 446                      <div class="nav-next"><?php echo $next_link; ?></div>
 447                  <?php endif; ?>
 448              </nav><!-- .navigation -->
 449              <?php
 450          endif;
 451      }
 452  endif;
 453  
 454  if ( ! function_exists( 'twentytwelve_comment' ) ) :
 455      /**
 456       * Template for comments and pingbacks.
 457       *
 458       * To override this walker in a child theme without modifying the comments template
 459       * simply create your own twentytwelve_comment(), and that function will be used instead.
 460       *
 461       * Used as a callback by wp_list_comments() for displaying the comments.
 462       *
 463       * @since Twenty Twelve 1.0
 464       *
 465       * @global WP_Comment $comment Global comment object.
 466       * @global WP_Post    $post    Global post object.
 467       *
 468       * @param WP_Comment $comment The comment object.
 469       * @param array      $args    An array of comment arguments. @see get_comment_reply_link()
 470       * @param int        $depth   The depth of the comment.
 471       */
 472  	function twentytwelve_comment( $comment, $args, $depth ) {
 473          $GLOBALS['comment'] = $comment;
 474          switch ( $comment->comment_type ) :
 475              case 'pingback':
 476              case 'trackback':
 477                  // Display trackbacks differently than normal comments.
 478                  ?>
 479          <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
 480          <p><?php _e( 'Pingback:', 'twentytwelve' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></p>
 481                  <?php
 482                  break;
 483              default:
 484                  // Proceed with normal comments.
 485                  global $post;
 486                  ?>
 487          <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
 488          <article id="comment-<?php comment_ID(); ?>" class="comment">
 489              <header class="comment-meta comment-author vcard">
 490                  <?php
 491                      echo get_avatar( $comment, 44 );
 492                      printf(
 493                          '<cite><b class="fn">%1$s</b> %2$s</cite>',
 494                          get_comment_author_link(),
 495                          // If current post author is also comment author, make it known visually.
 496                          ( $comment->user_id === $post->post_author ) ? '<span>' . __( 'Post author', 'twentytwelve' ) . '</span>' : ''
 497                      );
 498                      printf(
 499                          '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
 500                          esc_url( get_comment_link( $comment->comment_ID ) ),
 501                          get_comment_time( 'c' ),
 502                          /* translators: 1: Date, 2: Time. */
 503                          sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
 504                      );
 505                  ?>
 506                  </header><!-- .comment-meta -->
 507  
 508                  <?php
 509                  $commenter = wp_get_current_commenter();
 510                  if ( $commenter['comment_author_email'] ) {
 511                      $moderation_note = __( 'Your comment is awaiting moderation.', 'twentytwelve' );
 512                  } else {
 513                      $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.', 'twentytwelve' );
 514                  }
 515                  ?>
 516  
 517                  <?php if ( '0' === $comment->comment_approved ) : ?>
 518                  <p class="comment-awaiting-moderation"><?php echo $moderation_note; ?></p>
 519                  <?php endif; ?>
 520  
 521                  <section class="comment-content comment">
 522                  <?php comment_text(); ?>
 523                  <?php edit_comment_link( __( 'Edit', 'twentytwelve' ), '<p class="edit-link">', '</p>' ); ?>
 524                  </section><!-- .comment-content -->
 525  
 526                  <div class="reply">
 527                  <?php
 528                  comment_reply_link(
 529                      array_merge(
 530                          $args,
 531                          array(
 532                              'reply_text' => __( 'Reply', 'twentytwelve' ),
 533                              'after'      => ' <span>&darr;</span>',
 534                              'depth'      => $depth,
 535                              'max_depth'  => $args['max_depth'],
 536                          )
 537                      )
 538                  );
 539                  ?>
 540                  </div><!-- .reply -->
 541              </article><!-- #comment-## -->
 542                  <?php
 543                  break;
 544          endswitch; // End comment_type check.
 545      }
 546  endif;
 547  
 548  if ( ! function_exists( 'twentytwelve_entry_meta' ) ) :
 549      /**
 550       * Sets up post entry meta.
 551       *
 552       * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
 553       *
 554       * Create your own twentytwelve_entry_meta() to override in a child theme.
 555       *
 556       * @since Twenty Twelve 1.0
 557       */
 558  	function twentytwelve_entry_meta() {
 559          $categories_list = get_the_category_list( wp_get_list_item_separator() );
 560  
 561          $tags_list = get_the_tag_list( '', wp_get_list_item_separator() );
 562  
 563          $date = sprintf(
 564              '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
 565              esc_url( get_permalink() ),
 566              esc_attr( get_the_time() ),
 567              esc_attr( get_the_date( 'c' ) ),
 568              esc_html( get_the_date() )
 569          );
 570  
 571          $author = sprintf(
 572              '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
 573              esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
 574              /* translators: %s: Author display name. */
 575              esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
 576              get_the_author()
 577          );
 578  
 579          if ( $tags_list && ! is_wp_error( $tags_list ) ) {
 580              /* translators: 1: Category name, 2: Tag name, 3: Date, 4: Author display name. */
 581              $utility_text = __( 'This entry was posted in %1$s and tagged %2$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
 582          } elseif ( $categories_list ) {
 583              /* translators: 1: Category name, 3: Date, 4: Author display name. */
 584              $utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
 585          } else {
 586              /* translators: 3: Date, 4: Author display name. */
 587              $utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
 588          }
 589  
 590          printf(
 591              $utility_text,
 592              $categories_list,
 593              $tags_list,
 594              $date,
 595              $author
 596          );
 597      }
 598  endif;
 599  
 600  /**
 601   * Extends the default WordPress body classes.
 602   *
 603   * Extends the default WordPress body class to denote:
 604   * 1. Using a full-width layout, when no active widgets in the sidebar
 605   *    or full-width template.
 606   * 2. Front Page template: thumbnail in use and number of sidebars for
 607   *    widget areas.
 608   * 3. White or empty background color to change the layout and spacing.
 609   * 4. Custom fonts enabled.
 610   * 5. Single or multiple authors.
 611   *
 612   * @since Twenty Twelve 1.0
 613   *
 614   * @param array $classes Existing class values.
 615   * @return array Filtered class values.
 616   */
 617  function twentytwelve_body_class( $classes ) {
 618      $background_color = get_background_color();
 619      $background_image = get_background_image();
 620  
 621      if ( ! is_active_sidebar( 'sidebar-1' ) || is_page_template( 'page-templates/full-width.php' ) ) {
 622          $classes[] = 'full-width';
 623      }
 624  
 625      if ( is_page_template( 'page-templates/front-page.php' ) ) {
 626          $classes[] = 'template-front-page';
 627          if ( has_post_thumbnail() ) {
 628              $classes[] = 'has-post-thumbnail';
 629          }
 630          if ( is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) ) {
 631              $classes[] = 'two-sidebars';
 632          }
 633      }
 634  
 635      if ( empty( $background_image ) ) {
 636          if ( empty( $background_color ) ) {
 637              $classes[] = 'custom-background-empty';
 638          } elseif ( in_array( $background_color, array( 'fff', 'ffffff' ), true ) ) {
 639              $classes[] = 'custom-background-white';
 640          }
 641      }
 642  
 643      // Enable custom font class only if the font CSS is queued to load.
 644      if ( wp_style_is( 'twentytwelve-fonts', 'queue' ) ) {
 645          $classes[] = 'custom-font-enabled';
 646      }
 647  
 648      if ( ! is_multi_author() ) {
 649          $classes[] = 'single-author';
 650      }
 651  
 652      return $classes;
 653  }
 654  add_filter( 'body_class', 'twentytwelve_body_class' );
 655  
 656  /**
 657   * Adjusts content width in certain contexts.
 658   *
 659   * Adjusts content_width value for full-width and single image attachment
 660   * templates, and when there are no active widgets in the sidebar.
 661   *
 662   * @since Twenty Twelve 1.0
 663   *
 664   * @global int $content_width Content width.
 665   */
 666  function twentytwelve_content_width() {
 667      if ( is_page_template( 'page-templates/full-width.php' ) || is_attachment() || ! is_active_sidebar( 'sidebar-1' ) ) {
 668          global $content_width;
 669          $content_width = 960;
 670      }
 671  }
 672  add_action( 'template_redirect', 'twentytwelve_content_width' );
 673  
 674  /**
 675   * Registers postMessage support.
 676   *
 677   * Adds postMessage support for site title and description for the Customizer.
 678   *
 679   * @since Twenty Twelve 1.0
 680   *
 681   * @param WP_Customize_Manager $wp_customize Customizer object.
 682   */
 683  function twentytwelve_customize_register( $wp_customize ) {
 684      $wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
 685      $wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
 686      $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
 687  
 688      if ( isset( $wp_customize->selective_refresh ) ) {
 689          $wp_customize->selective_refresh->add_partial(
 690              'blogname',
 691              array(
 692                  'selector'            => '.site-title > a',
 693                  'container_inclusive' => false,
 694                  'render_callback'     => 'twentytwelve_customize_partial_blogname',
 695              )
 696          );
 697          $wp_customize->selective_refresh->add_partial(
 698              'blogdescription',
 699              array(
 700                  'selector'            => '.site-description',
 701                  'container_inclusive' => false,
 702                  'render_callback'     => 'twentytwelve_customize_partial_blogdescription',
 703              )
 704          );
 705      }
 706  }
 707  add_action( 'customize_register', 'twentytwelve_customize_register' );
 708  
 709  /**
 710   * Renders the site title for the selective refresh partial.
 711   *
 712   * @since Twenty Twelve 2.0
 713   *
 714   * @see twentytwelve_customize_register()
 715   *
 716   * @return void
 717   */
 718  function twentytwelve_customize_partial_blogname() {
 719      bloginfo( 'name' );
 720  }
 721  
 722  /**
 723   * Renders the site tagline for the selective refresh partial.
 724   *
 725   * @since Twenty Twelve 2.0
 726   *
 727   * @see twentytwelve_customize_register()
 728   *
 729   * @return void
 730   */
 731  function twentytwelve_customize_partial_blogdescription() {
 732      bloginfo( 'description' );
 733  }
 734  
 735  /**
 736   * Enqueues JavaScript postMessage handlers for the Customizer.
 737   *
 738   * Binds JS handlers to make the Customizer preview reload changes asynchronously.
 739   *
 740   * @since Twenty Twelve 1.0
 741   */
 742  function twentytwelve_customize_preview_js() {
 743      wp_enqueue_script( 'twentytwelve-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20250217', array( 'in_footer' => true ) );
 744  }
 745  add_action( 'customize_preview_init', 'twentytwelve_customize_preview_js' );
 746  
 747  /**
 748   * Modifies tag cloud widget arguments to display all tags in the same font size
 749   * and use list format for better accessibility.
 750   *
 751   * @since Twenty Twelve 2.4
 752   *
 753   * @param array $args Arguments for tag cloud widget.
 754   * @return array The filtered arguments for tag cloud widget.
 755   */
 756  function twentytwelve_widget_tag_cloud_args( $args ) {
 757      $args['largest']  = 22;
 758      $args['smallest'] = 8;
 759      $args['unit']     = 'pt';
 760      $args['format']   = 'list';
 761  
 762      return $args;
 763  }
 764  add_filter( 'widget_tag_cloud_args', 'twentytwelve_widget_tag_cloud_args' );
 765  
 766  if ( ! function_exists( 'wp_body_open' ) ) :
 767      /**
 768       * Fires the wp_body_open action.
 769       *
 770       * Added for backward compatibility to support pre-5.2.0 WordPress versions.
 771       *
 772       * @since Twenty Twelve 3.0
 773       */
 774  	function wp_body_open() {
 775          /**
 776           * Triggered after the opening <body> tag.
 777           *
 778           * @since Twenty Twelve 3.0
 779           */
 780          do_action( 'wp_body_open' );
 781      }
 782  endif;


Generated : Thu Jun 18 08:20:10 2026 Cross-referenced by PHPXref