Back to Top

Create CMS Page By Installer In Magento 2

Updated 28 February 2024

Create CMS Page By Installer In Magento 2 – Here I’m going to explain you how you can create cms page by patch data installer in magento2.
First of all create a “AddCustomCmsPage.php” file on location Vendor\Module\Setup\Patch\Data then use below code snippet.

<?php

namespace Vendor\Module\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Cms\Model\PageFactory;

class AddCustomCmsPage implements DataPatchInterface
{

    /**
     * @param PageFactory $pageFactory
     */
    public function __construct(
        PageFactory $pageFactory
    ) {
        $this->pageFactory = $pageFactory;
    }

    /**
     * @inheritdoc
     */
    public function apply()
    {
        $cmsPageData = [
            'title' => 'Custom cms page', // cms page title
            'page_layout' => '1column', // cms page layout
            'meta_keywords' => 'Page keywords', // cms page meta keywords
            'meta_description' => 'Page description', // cms page description
            'identifier' => 'custom-page', // cms page url identifier
            'content_heading' => 'Custom cms page', // Page heading
            'content' => "<h1>Write your custom cms page content.......</h1>", // page content
            'is_active' => 1, // define active status
            'stores' => [0], // assign to stores
            'sort_order' => 0 // page sort order
        ];
        // create page
        $this->pageFactory->create()->setData($cmsPageData)->save();
    }

    /**
     * @inheritdoc
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * @inheritdoc
     */
    public function getAliases()
    {
        return [];
    }
}

In this way we can create CMS page programmatically by installer file.Thanks..

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

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


1 comments

  • Vikas mehar
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home