Create CMS Block Programmatically Magento 2

Create CMS Block Programmatically Magento 2.

In Magento 2, CMS blocks are lines of HTML code that can be placed in multiple places and repeated usage.

We can also add these CMS blocks inside CMS Pages and other blocks.

In today’s tutorial, we are going to learn how we can create CMS blocks programmatically in Magento 2.

To create a CMS block you require the following values :

Title – The title of the CMS block
Identifier – the unique identifier key for the block
Store Views – defines the scope for the CMS Block
Status – Defines if the block is visible or not
Content – The HTML content that will be displayed while calling the CMS Block

To create a CMS Block programmatically we can use InstallData to achieve it.

I’m assuming you have already created your custom module, If not use this tutorial to quickly create a simple module in Magento 2.

Create the below file and update the content as per requirement :

Example: app/code/Jigar/Tutorials/Setup/InstallData.php

After the deployment, you will see the block is created in your Magento 2 instance.

You can check in the CMS Block grid by navigating to Content > Blocks Grid inside the Magento 2 admin panel.

CMS blocks are stored in the cms_block table and mapped with the cms_block_store table.

You can now use and render the create block using the below snippets.

From CMS Page :

From XML File:

For Example, cms_index_index.xml

Add the block using the identifier below :

Run the below commands to reflect changes :

That’s it, Now you have learned how to create CMS Blocks programmatically using Magento 2 modules.

It will be very useful when you have to create many CMS Blocks and need to import them in multiple instances, You will not required to manually move each CMS Blocks.

I hope this tutorial helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

Do share this tutorial with your team.

Thank you. Happy Coding !!

You may also like :

How to validate email address in Magento 2 programmatically?

Magento 2 : What is Router Id = admin & standard ?

Demonstrate ability to use and customize Categories in Magento 2

People also searched for :

  • cms block programatically magento 2
  • create cms block programmatically magento 2
  • magento 2 update cms block programmatically
  • magento 2 get cms block programmatically
  • magento cms block
  • create block programmatically magento 2
  • cms block
  • cms block in magento 2
  • magento 2 add block to cms page
  • magento 2 get cms block by identifier in phtml
  • magento dynamic blocks
  • magento 2 create block programmatically in phtml
  • magento 2 cms block in phtml
  • magento 2 blocks
  • magento 2 get cms block in phtml
  • add cms block in phtml magento 2
  • magento 2 block set template
  • add cms block in xml magento 2
  • cms block programatically magento 2
  • create cms block programmatically magento 2
  • magento 2 update cms block programmatically
  • magento 2 get cms block programmatically
  • magento cms block
  • create block programmatically magento 2
  • cms block
  • cms block in magento 2
  • magento 2 add block to cms page
  • magento 2 get cms block by identifier in phtml
  • magento dynamic blocks
  • magento 2 create block programmatically in phtml
  • magento 2 cms block in phtml
  • magento 2 blocks
  • magento 2 get cms block in phtml
  • add cms block in phtml magento 2
  • magento 2 block set template
  • add cms block in xml magento 2
  • cms block programatically magento 2
  • magento cms block
  • is magento a cms
  • magento 2 blocks
  • magento 2 update cms block programmatically
  • create block programmatically magento 2
  • magento 2 cms block in phtml
  • cms block in magento 2
  • add cms block in xml magento 2
  • magento 2 get cms block by identifier in phtml
  • create cms block programmatically magento 2
  • how to create cms block in magento 2
  • magento dynamic blocks
  • magento 2 check if cms block exists

Leave a Reply

Your email address will not be published. Required fields are marked *