Skip to content

Instantly share code, notes, and snippets.

@westcoastdigital
Created March 14, 2019 01:18
Show Gist options
  • Save westcoastdigital/6066f3e87c29be695ef2733a187447ac to your computer and use it in GitHub Desktop.
Save westcoastdigital/6066f3e87c29be695ef2733a187447ac to your computer and use it in GitHub Desktop.
Determines if a custom logo SVG format when uploaded to WordPress Customiser
/** Add SVG support first **/
function wcd_add_svg_support($file_types){
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg+xml';
$file_types = array_merge($file_types, $new_filetypes );
return $file_types;
}
add_action('upload_mimes', 'wcd_add_svg_support');
/*
* Determine logo and declare output
* to use in theme you just need to us the following code
* <?php wcd_logo(); ?>
*/
function wcd_logo()
{
/** Get our custom logo */
$custom_logo_id = get_theme_mod('custom_logo');
/** Get custom logo full url */
$logo = wp_get_attachment_image_src($custom_logo_id, 'full');
/** Get the extension of our logo ie: svg, jpg, png etc */
$extension = end(explode('.', $logo[0]));
/** first check if there is a logo uploaded */
if (has_custom_logo()) {
/** now check if it is an svg file */
if ( $extension != 'svg' ) {
/** if it is not svg, just output the image with homepage link */
$custom_logo = '<a href="' . get_site_url() . '" class="logo navbar-brand"><img src="' . esc_url($logo[0]) . '"></a>';
} else {
/** it is svg so lets get the svg code from the uploaded file */
$svg_file = file_get_contents($logo[0]);
/** output the svg inline for custom styling with homepage link */
$custom_logo = '<a href="' . get_site_url() . '" class="logo navbar-brand">' . $svg_file . '</a>';
}
} else {
/** there is no logo, so lets output the site title */
$custom_logo = '<a href="' . get_site_url() . '" class="navbar-brand">' . get_bloginfo('name') . '</a>';
}
echo $custom_logo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment