---
title: "How to Add Dynamic CMS Block Programmatically in Magento 2?"
url: "https://magecomp.com/blog/add-dynamic-cms-block-programmatically-magento-2/"
date: "2021-10-22T17:46:18+00:00"
modified: "2025-02-19T05:31:47+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 350
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "CMS Blocks are used to display content anywhere on the page. Learn How to Add Dynamic CMS Block Programmatically in Magento 2."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How to Add Download Invoice Button in Order Email?"
    url: "https://magecomp.com/blog/magento-2-add-download-invoice-button-in-order-email/"
  - title: "Top 10 Magento 2 Mobile App Builder Extensions in 2026"
    url: "https://magecomp.com/blog/magento-2-mobile-app-builders/"
  - title: "How to add a Custom Column to the Products Grid of Magento 2?"
    url: "https://magecomp.com/blog/add-custom-column-products-grid-magento-2/"
---

# How to Add Dynamic CMS Block Programmatically in Magento 2?

_Published: October 22, 2021_  
_Author: Dhiren Vasoya_  

![How to Add Dynamic CMS Block Programmatically in Magento 2](https://magecomp.com/blog/wp-content/uploads/2021/10/How-to-Add-Dynamic-CMS-Block-Programmatically-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

Today let’s learn How to Add Dynamic CMS Block Programmatically in Magento 2.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-4.webp)](https://magecomp.com/services/hyva-theme-development/)

Blocks can be placed anywhere on the page. Blocks that contain content are known as CMS Blocks. CMS Blocks are used to display text, video, or images. It can also be used to display dynamic information on the page. You can Add Dynamic CMS Block Programmatically in Magento 2. Let’s learn how
## Steps to Add Dynamic CMS Block Programmatically in Magento 2:
**Step 1:** Navigate to the below path
**app\code\Vendor\Extension\Setup\InstallData.php**

Now add the code as follows
```
namespace VendorExtensionSetup;

use MagentoCmsModelBlockFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $blockFactory;

    public function __construct(BlockFactory $blockFactory)
    {
        $this->blockFactory = $blockFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $cmsBlockData = [
            'title' => 'Custom Cms Block',
            'identifier' => 'custom_cms_block',
            'content' => 'Your Custom Text Here',
            'is_active' => 1,
            'stores' => [0],
            'sort_order' => 0
        ];

        $this->blockFactory->create()->setData($cmsBlockData)->save();
    }
}
```

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

**Step 2:** Now, Call CMS Block. For that go to the below path
**app\code\Vendor\Extension\view\frontend\templates\callcmsblock.phtml**

```
echo $block -> getLayout( )
                  -> createBlock('MagentoCmsBlockBlock')
                  -> setBlockId('custom_cms_block') // CMS block Identifier
          ->toHtml();
```

When you first install your module into Magento, at that time you need to Call CMS Block as given above.
**Step 3:** Now run the below commands
```
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento cache:flush
```

**Result:**

Your CMS block is created in the Admin Panel at the below path
**Content > Elements > Blocks**

[![CMS Block](https://magecomp.com/blog/wp-content/uploads/2021/10/CMS-Block.png)](https://magecomp.com/blog/wp-content/uploads/2021/10/CMS-Block.png)

## Conclusion:
Hence, accordingly, you can Add Dynamic CMS Block Programmatically in Magento 2. Also look into [**How to Create New CMS Block in Magento 2**](https://magecomp.com/blog/create-new-cms-block-in-magento-2/). If you face any difficulty with the implementation of the above steps, drop a comment. I will be back to you as quickly as possible. Do share the article with your friends and keep yourself updated!
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/add-dynamic-cms-block-programmatically-magento-2/](https://magecomp.com/blog/add-dynamic-cms-block-programmatically-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-29 12:46:59 UTC_  
