Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to get Logo url, Image Url, Alt text, Logo size in Magento 2

Vinh Jacker | 12-18-2024

Get logo url, alt text, logo size

Logo URL, ALT Text, Image URL, and Logo Size (include height and width) are the basic information you totally get when the logo is applied to your store. In specific, the logo URL is the direct link that will show the page whenever there is any person clicking on the logo.

These elements play an important role in increasing your brand identity and ensuring a good score in SEO ranking. That’s why you will need a perfect size for your logo, a relevant ALT text, and an optimized logo URL. Before that, you need to get all of the existing logo information to easily compare, optimize, update later.

And the logo size is the height and width of the logo image. What about the ALT Text? That is the content which is visible in the logo. Those parameters are open for you to get via this tutorial article if you are running Magento 2 platform on the store.

Main contents

Step 1: Declare in Mageplaza_HelloWorld

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of Logo class in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_logo;    
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Theme\Block\Html\Header\Logo $logo,
        array $data = []
    )
    {        
        $this->_logo = $logo;
        parent::__construct($context, $data);
    }
    
    /**
     * Get logo image URL
     *
     * @return string
     */
    public function getLogoSrc()
    {    
        return $this->_logo->getLogoSrc();
    }
    
    /**
     * Get logo text
     *
     * @return string
     */
    public function getLogoAlt()
    {    
        return $this->_logo->getLogoAlt();
    }
    
    /**
     * Get logo width
     *
     * @return int
     */
    public function getLogoWidth()
    {    
        return $this->_logo->getLogoWidth();
    }
    
    /**
     * Get logo height
     *
     * @return int
     */
    public function getLogoHeight()
    {    
        return $this->_logo->getLogoHeight();
    }    
}
?>

You can see more functions in vendor/magento/module-theme/Block/Html/Header/Logo.php.

Step 2: Declare function in template (.phtml) file

Run the below function in your template (.phtml) file

echo $block->getLogoSrc() . '<br />';
echo $block->getLogoAlt() . '<br />';
echo $block->getLogoWidth() . '<br />';
echo $block->getLogoHeight() . '<br />';

You can see more functions in vendor/magento/module-theme/Block/Html/Header/Logo.php.

Final words

The topic How to get logo url, alt text, and logo size in magento 2 ends here. I hope this is the helpful article when you want to fetch the logo information. In case that you have any question about the article, use the comment section below!

Related Post:

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services