Bobcares

Magento 2 Create CMS Block Programmatically | Guide

by | Nov 24, 2023

Learn how to create CMS block programmatically in Magento 2. Our Magento Support team is here to help you with your questions and concerns.

Magento 2 Create CMS Block Programmatically | Guide

Did you know that dynamically creating CMS blocks in Magento 2 helps us display tailored content on our pages?
In other words, it helps us showcase text, video, images, or even dynamic information.

Magento 2 Create CMS Block Programmatically | Guide

We can place blocks anywhere on the page. CMS Blocks can help display text, video, or images.

Today, we are going to take a look at how to Dynamic CMS Block Programmatically in Magento 2:

  1. To begin with, we need a custom module. This includes creating essential files and folders, including registration.php and module.xml.
  2. Then, we have to create a PHP file. This file should be located in our module’s directory.
  3. Now, we must write code to create a CMS block programmatically.

    For example:


    <?php
    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php';
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    // Load the store manager
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
    $storeId = $storeManager->getStore()->getId();
    // Load the block factory
    $blockFactory = $objectManager->get('\Magento\Cms\Model\BlockFactory');
    // Create a new CMS block
    $blockData = [
    'title' => 'Your Block Title',
    'identifier' => 'your_block_identifier',
    'content' => 'Your block content goes here.',
    'is_active' => 1,
    'stores' => [$storeId],
    ];
    $block = $blockFactory->create();
    $block->setData($blockData)->save();
    echo 'CMS Block has been created successfully.';

  4. Finally, it is time to customize and run the script.

    In the above example, we have a $blockData array. We can modify the values in this array to meet our needs.

    Then, we can run the script either through the command line by heading to the directory with the PHP file and running this command:

    php our_php_file.php

    Alternatively, we can access it via a browser as well.

  5. Now, we will be able to see the newly created CMS block in the Magento admin panel under Content > Elements > Blocks.

Let us know if you need further help with creating CMS blocks.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to create CMS block programmatically in Magento 2.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.