Instantly share code, notes, and snippets.

@smutek /brand.php
Last active Jan 20, 2018

Embed
What would you like to do?
Filter WordPress Custom Logo
<?php
/**
* Site Brand
*
* Output site branding
*
* Use native WordPress site logo with custom (bootstrap friendly) markup
* Falls back to text title if logo is not set.
*
* @param $html
*
* @return string
*/
function siteBrand($html)
{
// grab the site name as set in customizer options
$site = get_bloginfo('name');
// Wrap the site name in an H1 if on home, in a paragraph tag if not.
is_front_page() ? $title = '<h1>' . $site . '</h1>' : $title = '<p>' . $site . '</p>';
// Grab the home URL
$home = esc_url(home_url('/'));
// Class for the link
$class = 'navbar-brand';
// Set anchor content to $title
$content = $title;
// Check if there is a custom logo set in customizer options...
if (has_custom_logo()) {
// get the URL to the logo
$logo = wp_get_attachment_image(get_theme_mod('custom_logo'), 'full', false, array(
'class' => 'brand-logo img-responsive',
'itemprop' => 'logo',
));
// we have a logo, so let's update the $content variable
$content = $logo;
// include the site name markup, hidden with screen reader friendly styles
$content .= '<span class="sr-only">' . $title . '</span>';
}
// construct the final html
$html = sprintf('<a href="%1$s" class="%2$s" rel="home" itemprop="url">%3$s</a>', $home, $class, $content);
// return the result to the front end
return $html;
}
add_filter('get_custom_logo', __NAMESPACE__ . '\\siteBrand');
<header class="banner navbar navbar-inverse navbar-static-top" role="banner">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only"><?= __('Toggle navigation', 'sage'); ?></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<?php the_custom_logo(); ?>
</div>
<nav class="collapse navbar-collapse" role="navigation">
<?php
if (has_nav_menu('primary_navigation')) :
wp_nav_menu(['theme_location' => 'primary_navigation', 'menu_class' => 'nav navbar-nav']);
endif;
?>
</nav>
</div>
</header>
<?php
// Enable custom logo support
// https://codex.wordpress.org/Theme_Logo
add_theme_support( 'custom-logo' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment