how to add new Block on login page magento2?

Add new block by creating a new module. for example :

Make a new directory in app/code/

In this create another folders Foo/Bar

For creating module, create module.xml in Foo/Bar/etc/module.xml.

And paste this code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Foo_Bar" setup_version="2.0.1"/>
</config>

Run this php bin/magento setup:upgrade in your CLI for create module.

Create Foo/Bar/Block/Baz.php

and paste this code

 <?php 
   namespace Foo\Bar\Block; 
 class Baz
    extends \Magento\Framework\View\Element\Template
 {
    public function getTitle()
 {
    return "New Block";
  }
}

create Foo/Bar/view/frontend/layout/customer_account_login.xml

and paste this code

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Foo\Bar\Block\Baz" template="Foo_Bar::default/baz.phtml"/>
    </referenceContainer>
</body>
</page>

create Foo/Bar/view/frontend/templates/default/baz.phtml

and paste this code

<?php
?>
  <h1tag><?php echo $block->getTitle(); ?></h1tag>

Remove cache and check your block by reloading login page.


In Admin Panel : go to content > widgets > Add Widget > then

enter image description here

click on the continue button

enter image description here

and select the block you want to add.


Add it via widget, Add a new widget of CMS static block type and than in widget options select your specific page.

Tags:

Magento2