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

Magento 2 create CMS page programmatically

Vinh Jacker | 03-17-2025

Create CMS pages programmatically

Creating a CMS page programmatically in Magento 2 is also a crucial method you need to learn. As soon as creating CMS page successfully, you will have full control of the management of the content on the page. In a CMS page, you can edit or update or delete any data as need. With CMS page, it is flexible to intervene into the change of the content. In previous post, we talk about Create a CMS page in Admin, you also can follow the post to create a sample CMS page in backend.

In this topic, you can know how to insert CMS page programmatically into Magento 2 setup script through specific steps:

4 Steps to create CMS Page programmatically

Step 1: Create UpgradeData.php file

Generate Setup/UpgradeData.php file in your module.

Step 2: Insert UpgradeData class

Use UpgradeData class as the below, then add required model with dependency injection and a code script to create a new CMS page.

<?php
 
namespace Vendor\Module\Setup;
 
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
 
/**
 * @codeCoverageIgnore
 */
class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var \Magento\Cms\Model\PageFactory
     */
    protected $_pageFactory;
 
    /**
     * Construct
     *
     * @param \Magento\Cms\Model\PageFactory $pageFactory
     */
    public function __construct(
        \Magento\Cms\Model\PageFactory $pageFactory
    ) {
        $this->_pageFactory = $pageFactory;
    }
 
    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
 
        if (version_compare($context->getVersion(), '1.1') < 0) {
            $page = $this->_pageFactory->create();
            $page->setTitle('Example CMS page')
                ->setIdentifier('example-cms-page')
                ->setIsActive(true)
                ->setPageLayout('1column')
                ->setStores(array(0))
                ->setContent('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
                ->save();
        }
 
        $setup->endSetup();
    }
}

Step 3: Setup the module version

Open etc/module.xml file, you need to set your module version in setup_version attribute that should be 1.1

Step 4: Run the upgrade script

Finally, run the following database upgrade script, then you will complete the creating new CMS page on your Magento 2 storefront.

bin/magento setup:upgrade

That’s all steps that allow you to create a new CMS page programmatically in Magento 2. Thank you so much for the watching!

Related Post:

Wrap up

That’s all steps to create a new CMS page programmatically in Magento 2. I hope this tutorial is helpful for you. Don’t forget to let me know if you have any questions and share it with your friends if they also need.

x
    Jacker

    Jacker is the Chief Technology Officer (CTO) at Mageplaza, bringing over 10 years of experience in Magento, Shopify, and other eCommerce platforms. With deep technical expertise, he has led numerous successful projects, optimizing and scaling online stores for global brands. Beyond his work in eCommerce development, he is passionate about running and swimming.



    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