Tagged: , ,

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1306113
    #1306515

    Hey 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,
    Mike

    #1306522

    Hi 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');
    #1306538

    Hi,
    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,
    Mike

    #1306539

    Hi 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
    Christian

    #1306572

    you 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 clauses

    you 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;
    }
    #1306577

    Thanks @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.

    #1306579

    copy – 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 script

    function 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, 5 months ago by Guenni007.
    #1306619

    A 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/

    #1306662

    Hi,

    Thanks for helping out @guenni007 :-)

    Best regards,
    Rikard

    #1306674

    If 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.

    #1306797

    Hi,

    Thanks for your help as always @guenni007 :)

    Best regards,
    Yigit

    #1307101

    there 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; 
    }
Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.