WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/24/2016 10:09:54 PM (4 years ago)
Author:
obenland
Message:

Customize: Introduce Logo support for themes.

Allows a common theme feature to have a common implementation provided by core and available in a consistent location for users.
See https://make.wordpress.org/core/2016/02/24/theme-logo-support/

Props kwight, enejb, jeherve, bhubbard, samhotchkiss, zinigor, eliorivero, adamsilverstein, melchoyce, ryan, mikeschroder, westonruter, pento, karmatosed, celloexpressions, obenland.
See #33755.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r36693 r36698  
    830830function has_site_icon( $blog_id = 0 ) {
    831831    return (bool) get_site_icon_url( 512, '', $blog_id );
     832}
     833
     834/**
     835 * Whether the site has a Site Logo.
     836 *
     837 * @since 4.5.0
     838 *
     839 * @param int $blog_id Optional. ID of the blog in question. Default current blog.
     840 * @return bool Whether the site has a site logo or not.
     841 */
     842function has_site_logo( $blog_id = 0 ) {
     843    if ( is_multisite() && (int) $blog_id !== get_current_blog_id() ) {
     844        switch_to_blog( $blog_id );
     845    }
     846
     847    $site_logo_id = get_theme_mod( 'site_logo' );
     848
     849    if ( is_multisite() && ms_is_switched() ) {
     850        restore_current_blog();
     851    }
     852
     853    return (bool) $site_logo_id;
     854}
     855
     856/**
     857 * Returns a Site Logo, linked to home.
     858 *
     859 * @since 4.5.0
     860 *
     861 * @param int $blog_id Optional. ID of the blog in question. Default current blog.
     862 * @return string Site logo markup.
     863 */
     864function get_the_site_logo( $blog_id = 0 ) {
     865    $html = '';
     866
     867    if ( is_multisite() && (int) $blog_id !== get_current_blog_id() ) {
     868        switch_to_blog( $blog_id );
     869    }
     870
     871    $site_logo_id = get_theme_mod( 'site_logo' );
     872
     873    if ( is_multisite() && ms_is_switched() ) {
     874        restore_current_blog();
     875    }
     876    $size = get_theme_support( 'site-logo' );
     877    $size = $size[0]['size'];
     878
     879    // We have a logo. Logo is go.
     880    if ( $site_logo_id ) {
     881        $html = sprintf( '<a href="%1$s" class="site-logo-link" rel="home" itemprop="url">%2$s</a>',
     882            esc_url( home_url( '/' ) ),
     883            wp_get_attachment_image( $site_logo_id, $size, false, array(
     884                'class'     => "site-logo attachment-$size",
     885                'data-size' => $size,
     886                'itemprop'  => 'logo',
     887            ) )
     888        );
     889    }
     890
     891    // If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
     892    elseif ( is_customize_preview() ) {
     893        $html = sprintf( '<a href="%1$s" class="site-logo-link" style="display:none;"><img class="site-logo" data-size="%2$s" /></a>',
     894            esc_url( home_url( '/' ) ),
     895            esc_attr( $size )
     896        );
     897    }
     898
     899    /**
     900     * Filter the Site Logo output.
     901     *
     902     * @since 4.5.0
     903     *
     904     * @param string $html Site Logo HTML output.
     905     * @param string $size Size specified in add_theme_support declaration, or 'thumbnail' default.
     906     */
     907    return apply_filters( 'get_the_site_logo', $html, $size );
     908}
     909
     910/**
     911 * Displays a Site Logo, linked to home.
     912 *
     913 * @since 4.5.0
     914 *
     915 * @param int $blog_id Optional. ID of the blog in question. Default current blog.
     916 */
     917function the_site_logo( $blog_id = 0 ) {
     918    echo get_the_site_logo( $blog_id );
    832919}
    833920
Note: See TracChangeset for help on using the changeset viewer.