-
AuthorPosts
-
June 17, 2021 at 5:25 am #1306113
Hi, is there a way to replace the image logo uploaded via Theme Options with inline svg code?
I’ve referenced the following https://kriesi.at/documentation/enfold/logo/ but it appears to only show how to change the logo as an img, not replace the img tag altogether with an svg tag.
June 19, 2021 at 5:08 pm #1306515Hey JoeSurf,
Thank you for your patience, unfortunately, at this time this is not possible as WordPress doesn’t support SVG files natively. Once WordPress supports SVG it should be as simple as uploading the file.Best regards,
MikeJune 19, 2021 at 6:29 pm #1306522Hi Mike, I was able to get WordPress to support both SVG files uploaded and SVG inline code.
So with SVG supported is there a way for me to modify the $logo in Functions.php to allow me to insert inline svg in place of the default img element. The documentation on your site shares the ability to change the logo via ‘av_change_logo_img’ but it’d be really helpful to be able to change the entire logo form img to inline svg.
The documentation shares the following to “Add Multiple Logos”:
function add_logos($logo) { if(is_page(home)){ $logo .= '<a class="first-logo" href="/"><img src="https://www.domain.com/logo-1.png"/></a>'; } return $logo; } add_filter('avf_logo_final_output', 'add_logos');
It seems like this comes close to providing a solution, however this only adds the logo to the homepage where as I would like to replace the default logo on every page.
Thanks in advance for helping figure this out.
For anyone interested, here’s the code I added to Functions.php to enable SVG support on WordPress. This first function enables the ability to add inline svg tags within the TinyMCE WYSIWYG html editor
function override_mce_options($initArray) { $opts = '*[*],svg[*],path[*],use[*],symbol[*],defs[*]'; $initArray['valid_elements'] = $opts; $initArray['extended_valid_elements'] = $opts; return $initArray; } add_filter('tiny_mce_before_init', 'override_mce_options');
This function allows the ability to upload SVG files into Media Library.
function add_file_types_to_uploads($file_types){ $new_filetypes = array(); $new_filetypes['svg'] = 'image/svg'; $file_types = array_merge($file_types, $new_filetypes ); return $file_types; } add_filter('upload_mimes', 'add_file_types_to_uploads');
June 19, 2021 at 11:47 pm #1306538Hi,
I believe that if the SVG files were truly supported by WordPress then there would be no need to modify the $logo in Functions.php, I will submit this to the rest of the team to see if they have any suggestions.Best regards,
MikeJune 20, 2021 at 12:24 am #1306539Hi JoeSurf,
with the “SVG Support” plugin you can include an SVG instead of a PNG or JPG as a logo in the theme options.
Best regards
ChristianJune 20, 2021 at 6:09 pm #1306572you can use this snippet in child-theme functions.php to place an svg inline instead of the existing img:
// insert svg as logo function replace_logo_with_svg() { ?> <script type="text/javascript"> (function($) { function a() { $( ".logo a img" ).remove(); $.get('/wp-content/uploads/YOUR-LOGO.svg', function(svg){ $( ".logo a" ).html( svg ); }, 'text'); } a(); })(jQuery); </script> <?php } add_action('wp_footer', 'replace_logo_with_svg');
I use svg support too – but to replace the logo via the “force replace” of that plugin might be to slow
if you want that only on special pages – you can use if clausesyou need to set some absolute width to the inlins svg logo – and for shrinking headers the display options had to be changed.
f.e.:.logo svg { width: 450px; } .logo a { display: flex; position: relative; }
June 20, 2021 at 7:33 pm #1306577Thanks @Guenni007!
That did the trick. Trying to wrap my head around your fix. I would not have gotten there on my own. But it works perfectly. I was thinking I was going to have to add the code manually within a function but your fix lets you upload the SVG within Media Library and just pull the file URL, which very handy.
June 20, 2021 at 8:34 pm #1306579copy – or more generally in use – it checks if the inserted logo ( if header transparency logo too ) is svg – then replaces it.
to allow the mime type you can use this littel snippet – or better use svg support:function custom_mtypes( $m ){ $m['svg'] = 'image/svg+xml'; $m['svgz'] = 'image/svg+xml'; return $m; } add_filter( 'upload_mimes', 'custom_mtypes' );
here is a new code:
( it takes the svg files inserted in the enfold options dialogs ( logo and transparency logo ) as source for the scriptfunction replace_logo_if_svg_with_inline_svg() { ?> <script type="text/javascript"> (function($) { $('.logo img').filter(function() { return this.src.match(/.*\.svg$/); }).each(function(){ var imgLogo = $(this); var imgURL = $(this).attr('src'); $.get(imgURL, function(data) { var svg = $(data).find('svg'); // Remove any invalid XML tags as per http://validator.w3.org svg = svg.removeAttr('xmlns xmlns:xlink'); // Replace image with new SVG imgLogo.replaceWith(svg); }, 'xml'); }); })(jQuery); </script> <?php } add_action('wp_footer', 'replace_logo_if_svg_with_inline_svg');
- This reply was modified 3 years, 6 months ago by Guenni007.
June 20, 2021 at 10:02 pm #1306619A few things to keep in mind:
If both the logo and the logo for the transparencies is an inline svg.logo, .logo a { overflow: visible; } .logo svg { position: absolute; left: 0; top: 0; width: 450px /*** an absolute value might be necessary for best browser support ***/ } #top :not(.av_header_transparency).av_alternate_logo_active .logo .subtext svg { opacity:0; filter:alpha(opacity=0) }
the svg code inside in the head section :
<svg alt="transparent Logo Webers Webdesign" title="Webers Webdesign" version="1.1" id="Webdesign-transparent" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100%" height="100%" viewBox="0 0 570 200" preserveAspectRatio="xMinYMid meet" xml:space="preserve">
- if width and height is set it will be responsive to the surrounding container.
- the preserveAspectRatio setting determines the way the svg shrinks with its surrounding container (xMinYMid – means it shrinks to the left side but stays in the middle on y-Achsis)
if this is the fact – you do not need to set different display options for the .logo and .logo a
see here: https://webers-testseite.de/June 21, 2021 at 7:18 am #1306662June 21, 2021 at 8:35 am #1306674If you like to replace all imgs that are svg files with its inline code without plugin – it might be nice to preserve ( or set ) title, alt and class attributes.
I wouldn’t change the inline svg ID.
Put this to your child-theme functions.php:// replace every svg with its inline code function replace_img_if_svg_with_inline_svg() { ?> <script type="text/javascript"> (function($) { $('img').filter(function() { return this.src.match(/.*\.svg$/); }).each(function(){ var imgSVG = $(this); var imgURL = $(this).attr('src'); var imgAlt = $(this).attr('alt'); var imgTitle = $(this).attr('title'); var imgClass = $(this).attr('class'); $.get(imgURL, function(data) { var svg = $(data).find('svg'); // Remove invalid XML tags svg = svg.removeAttr('xmlns xmlns:xlink'); // add replaced image's alt tag to the inline SVG typeof imgAlt === 'undefined' || imgAlt === '' ? (svg = svg.attr('alt', 'Replaced Image')) : (svg = svg.attr('alt', imgAlt)) ; // add replaced image's Title tag to the inline SVG typeof imgTitle === 'undefined' || imgTitle === '' ? (svg = svg.attr('title', 'A SVG Image replaced by its inline code')) : (svg = svg.attr('title', imgTitle)); // Add replaced image's classes to the new SVG and add replaced-svg as new class typeof imgClass === 'undefined' || imgClass === '' ? (svg = svg.attr('class', 'replaced-svg')) : (svg = svg.attr('class', imgClass+' replaced-svg')); // Replace image with inline SVG Code imgSVG.replaceWith(svg); }, 'xml'); }); })(jQuery); </script> <?php } add_action('wp_footer', 'replace_img_if_svg_with_inline_svg');
This code snippet takes into account the assignment of the attributes within the ALB element.
June 21, 2021 at 6:12 pm #1306797June 23, 2021 at 9:51 am #1307101there are two if clauses to add. I told you above that the svg attribute of preserveAspectRatio decides how the svg react on shrinking.
so if your logo is on the right side ( a class exists on html : html_logo_right – and if the logo is centered it has on html the class: html_logo_center
so code of changing all img svgs goes to inline-svgs has two check if img is a logo img and if html got this class – and change that attribute:// replace every svg with its inline code function replace_img_if_svg_with_inline_svg() { ?> <script type="text/javascript"> (function($) { $('img').filter(function() { return this.src.match(/.*\.svg$/); }).each(function(){ var imgSVG = $(this); var imgURL = $(this).attr('src'); var imgAlt = $(this).attr('alt'); var imgTitle = $(this).attr('title'); var imgClass = $(this).attr('class'); $.get(imgURL, function(data) { var svg = $(data).find('svg'); // Remove invalid XML tags svg = svg.removeAttr('xmlns xmlns:xlink'); // add replaced image's alt tag to the inline SVG typeof imgAlt === 'undefined' || imgAlt === '' ? (svg = svg.attr('alt', 'Replaced Image')) : (svg = svg.attr('alt', imgAlt)) ; // add replaced image's Title tag to the inline SVG typeof imgTitle === 'undefined' || imgTitle === '' ? (svg = svg.attr('title', 'A SVG Image replaced by its inline code')) : (svg = svg.attr('title', imgTitle)); // Add replaced image's classes to the new SVG and add replaced-svg as new class typeof imgClass === 'undefined' || imgClass === '' ? (svg = svg.attr('class', 'replaced-svg')) : (svg = svg.attr('class', imgClass+' replaced-svg')); // check where the logo is placed : left or right or centered in the header if ($(imgSVG).parents('.logo').length && $('html').hasClass('html_logo_left')) { svg.attr('preserveAspectRatio', 'xMinYMin meet'); }; if ($(imgSVG).parents('.logo').length && $('html').hasClass('html_logo_right')) { svg.attr('preserveAspectRatio', 'xMaxYMin meet'); }; if ($(imgSVG).parents('.logo').length && $('html').hasClass('html_logo_center')) { svg.attr('preserveAspectRatio', 'xMidYMin meet'); }; // Replace image with inline SVG Code imgSVG.replaceWith(svg); }, 'xml'); }); })(jQuery); </script> <?php } add_action('wp_footer', 'replace_img_if_svg_with_inline_svg');
you can see here on my test-page that if the logo is on the right – it shrinks to the right:
https://webers-testseite.de/impressum/datenschutzerklaerung/
here is the css code for “nearly” all instances ( header left, center, right with transparency or not ):.logo, .logo a { overflow: visible; } .logo a { display: flex; } #top .logo svg { position: relative; left: 0; top: 0; width: 450px; } #top .logo .subtext svg { position: absolute; } .html_logo_right #top .logo svg { left: auto; right: 0; } .html_logo_center #top .logo > svg { -webkit-transform: translateX(-50%); transform: translateX(-50%); } #top .av_header_transparency.av_alternate_logo_active .logo a > svg { opacity: 0; filter: alpha(opacity=0); } #top :not(.av_header_transparency).av_alternate_logo_active .logo .subtext svg { opacity: 0; filter: alpha(opacity=0); } .av_header_transparency .logo .subtext svg { opacity: 1; }
-
AuthorPosts
- You must be logged in to reply to this topic.